Tricks Device (hdu 5294 最短路+最大流)

Tricks Device (hdu 5294 最短路+最大流)

大家好,又见面了,我是全栈君。

Tricks Device

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 124    Accepted Submission(s): 27

Problem Description
Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the entrance of the tomb while Dumb Zhang’s at the end of it. The tomb is made up of many chambers, the total number is N. And there are M channels connecting the chambers. Innocent Wu wants to catch up Dumb Zhang to find out the answers of some questions, however, it’s Dumb Zhang’s intention to keep Innocent Wu in the dark, to do which he has to stop Innocent Wu from getting him. Only via the original shortest ways from the entrance to the end of the tomb costs the minimum time, and that’s the only chance Innocent Wu can catch Dumb Zhang.

Unfortunately, Dumb Zhang masters the art of becoming invisible(奇门遁甲) and tricks devices of this tomb, he can cut off the connections between chambers by using them. Dumb Zhang wanders how many channels at least he has to cut to stop Innocent Wu. And Innocent Wu wants to know after how many channels at most Dumb Zhang cut off Innocent Wu still has the chance to catch Dumb Zhang.

 

Input
There are multiple test cases. Please process till EOF.

For each case,the first line must includes two integers, N(<=2000), M(<=60000). N is the total number of the chambers, M is the total number of the channels.

In the following M lines, every line must includes three numbers, and use ai、bi、li as channel i connecting chamber ai and bi(1<=ai,bi<=n), it costs li(0<li<=100) minute to pass channel i.

The entrance of the tomb is at the chamber one, the end of tomb is at the chamber N.

 

Output
Output two numbers to stand for the answers of Dumb Zhang and Innocent Wu’s questions.
 

Sample Input
   
   
8 9 1 2 2 2 3 2 2 4 1 3 5 3 4 5 4 5 8 1 1 6 2 6 7 5 7 8 1

 

Sample Output
   
   
2 6

 

Source

Recommend

题意:n个点m条无向边,如果从起点0到终点n-1的最短路距离为dist,求最少删除多少条边使得图中不再存在最短路。最多删除多少条边使得图中仍然存在最短路。

思路:先用spfa求一次最短路,开一个road数组,road[i]表示从起点走到i点最短路径所经过的最少边数,然后第二问就是m-road[n-1];再依据最短路的dist数组推断哪些边是最短路上的,用它们又一次构图。跑一遍网络流求最小割。比赛的时候没有在最短路上建边,直接用的原图。果断TLE,又坑了队友=-=

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int MAXN = 2005;
const int MAXM = 200005;
const int N = 1005;

int n,m;

struct EDGE
{
    int u,v,len,next;
}e[MAXM];

struct Edge
{
    int to,next,cap,flow;
}edge[MAXM];

int tol;
int head[MAXN];

void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}

void add(int u,int v,int len)
{
    e[tol].u=u;
    e[tol].v=v;
    e[tol].len=len;
    e[tol].next=head[u];
    head[u]=tol++;
    e[tol].u=v;
    e[tol].v=u;
    e[tol].len=len;
    e[tol].next=head[v];
    head[v]=tol++;
}

void addedge(int u,int v,int w,int rw=0)
{
    edge[tol].to=v;
    edge[tol].cap=w;
    edge[tol].flow=0;
    edge[tol].next=head[u];
    head[u]=tol++;

    edge[tol].to=u;
    edge[tol].cap=rw;
    edge[tol].flow=0;
    edge[tol].next=head[v];
    head[v]=tol++;
}

int Q[MAXN];
int dep[MAXN],cur[MAXN],sta[MAXN];

bool bfs(int s,int t,int n)
{
    int front=0,tail=0;
    memset(dep,-1,sizeof(dep[0])*(n+1));
    dep[s]=0;
    Q[tail++]=s;
    while (front<tail)
    {
        int u=Q[front++];
        for (int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if (edge[i].cap>edge[i].flow && dep[v]==-1)
            {
                dep[v]=dep[u]+1;
                if (v==t) return true;
                Q[tail++]=v;
            }
        }
    }
    return false;
}

int dinic(int s,int t,int n)
{
    int maxflow=0;
    while (bfs(s,t,n))
    {
        for (int i=0;i<n;i++) cur[i]=head[i];
        int u=s,tail=0;
        while (cur[s]!=-1)
        {
            if (u==t)
            {
                int tp=INF;
                for (int i=tail-1;i>=0;i--)
                    tp=min(tp,edge[sta[i]].cap-edge[sta[i]].flow);
                maxflow+=tp;
                for (int i=tail-1;i>=0;i--)
                {
                    edge[sta[i]].flow+=tp;
                    edge[sta[i]^1].flow-=tp;
                    if (edge[sta[i]].cap-edge[sta[i]].flow==0)
                        tail=i;
                }
                u=edge[sta[tail]^1].to;
            }
            else if (cur[u]!=-1 && edge[cur[u]].cap > edge[cur[u]].flow &&dep[u]+1==dep[edge[cur[u]].to])
            {
                sta[tail++]=cur[u];
                u=edge[cur[u]].to;
            }
            else
            {
                while (u!=s && cur[u]==-1)
                    u=edge[sta[--tail]^1].to;
                cur[u]=edge[cur[u]].next;
            }
        }
    }
    return maxflow;
}

int dist[MAXN];
int vis[MAXN];
int road[MAXN];

void SPFA()
{
    memset(vis,0,sizeof(vis));
    memset(dist,INF,sizeof(dist));
    memset(road,INF,sizeof(road));
    dist[0]=0;
    road[0]=0;
    vis[0]=1;
    queue<int>Q;
    Q.push(0);
    while (!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        vis[u]=0;
        for (int i=head[u];~i;i=e[i].next)
        {
            int v=e[i].v;
            if (dist[v]>dist[u]+e[i].len)
            {
                dist[v]=dist[u]+e[i].len;
                road[v]=road[u]+1;
                if (!vis[v])
                {
                    vis[v]=1;
                    Q.push(v);
                }
            }
            else if (dist[v]==dist[u]+e[i].len)
            {
                if (road[v]>road[u]+1)
                {
                    road[v]=road[u]+1;
                    if (!vis[v])
                    {
                        vis[v]=1;
                        Q.push(v);
                    }
                }
            }
        }
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
    int i,j,u,v,w;
    while (~sff(n,m))
    {
        init();
        for (i=0;i<m;i++)
        {
            sfff(u,v,w);
            if (u==v) continue;
            u--;v--;
            add(u,v,w);
        }
        SPFA();
        int cnt=tol;
        init();
        for (i=0;i<cnt;i++)
        {
            u=e[i].u;
            v=e[i].v;
            if (dist[v]==dist[u]+e[i].len)
                addedge(u,v,1);
        }
        int ans=dinic(0,n-1,n);
        pf("%d %d\n",ans,m-road[n-1]);
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/115451.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • “狗屁不通文章生成器”登顶GitHub热榜,分分钟写出万字形式主义大作

    “狗屁不通文章生成器”登顶GitHub热榜,分分钟写出万字形式主义大作一、垃圾文字生成器介绍最近在浏览GitHub的时候,发现了这样一个骨骼清奇的雷人项目,而且热度还特别高。项目中文名:狗屁不通文章生成器 项目英文名:BullshitGenerator根据作者的介绍,他是偶尔需要一些中文文字用于GUI开发时测试文本渲染,因此开发了这个废话生成器。但由于生成的废话实在是太过富于哲理,所以最近已经被小伙伴们给玩坏了。他的文风可能是这样的:你发现,…

  • linux java卸载再安装

    linux java卸载再安装先卸载原有的rpm-qa|grepjava#找到已存在的,删掉:rpm-e-nodeps+文件名删掉yum-ylistjava*yum-yinstall#在找到上面需要到版本来安装

  • MybatisCodeHelperPro永久激活码2022_最新在线免费激活

    (MybatisCodeHelperPro永久激活码2022)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

  • CSS3与页面布局学习总结(四)——页面布局的多种方法

    CSS3与页面布局学习总结(四)——页面布局的多种方法一、负边距与浮动布局1.1、负边距所谓的负边距就是margin取负值的情况,如margin:-100px,margin:-100%。当一个元素与另一个元素margin取负值时将拉近距离。常见的功能

  • 单片机中步进电机c语言程序,用AT89C51单片机控制步进电机的汇编源程序

    单片机中步进电机c语言程序,用AT89C51单片机控制步进电机的汇编源程序下面程序完成的主要功能:实现步进电机的正反转,加速、减速;显示电机转速(转速级别)和工作状态(正转、反转、不转)。源程序SPEEDEQU10H;SPEED为转速等级标志,共7级,即1~7FXEQU11H;FX为方向标志COUNTEQU12H;COUNT中断次数标志ORG0000HAJMPMAINORG0003H;外部中断0入口地址,加速子程序AJMPUPORG001…

  • Macbook pro/air 2013 late -2014 使用转接卡更换NVME SSD休眠不醒问题的解决办法

    Macbook pro/air 2013 late -2014 使用转接卡更换NVME SSD休眠不醒问题的解决办法、、1.手上512GMBP2013late差不多满了,因为穷,所以在淘宝上买了一个NVME转Macbookpcie,然后再买一个NVME2T的硬盘2.NVME因为需要最新的FirmwareRom支持,所以必须使用原装的硬盘(必须原装)安装Mac14以上,我安装了14.5.要不然识别不出来新安装的NVME硬盘3.买之前就知道是会有休眠问题的,问了卖家推荐了一些型号说不…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号