HDU 5294 Tricks Device 最短路建图+最小割(最大流)

来源:互联网 发布:清风dj软件下载 编辑:程序博客网 时间:2024/05/02 00:13

题目描述:

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 91 2 22 3 22 4 13 5 34 5 45 8 11 6 26 7 57 8 1 

Sample Output

2 6 

题目分析:

题意:有n个墓穴,有m条隧道,每条隧道通过需要一个特定的时间。吴邪在1点,张起灵在n点。吴邪需要走到n点,但是只能走这整个隧道的最短路。问:
张起灵最少需要将多少条隧道堵住,吴邪就走不到自己这里;
吴邪在最多多少条隧道被堵住的情况下,依然能走到张起灵那里。(马丹这题好傲娇)

这题是一个最短路+网络流的综合题,代码特别长(0.0)。我们先需要用一遍spfa找最短路,但是这个找最短路需要计算这些最短路里(因为有可能有多条)通过隧道数量最少的那一条。(因为这就能满足答案要求的最少和最多)
然后将这条最短路重新建图,用dinic算出其最小割(最大流),这个值就是张起灵的答案。吴邪的答案就是总共m条隧道减去那条被建图的最短路走过的隧道数。

代码如下:

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;typedef long long ll;const int INF = 0x3f3f3f3f;const double eps = 1e-4;const int MAXN=2010;const int MAXM=120010;//别忘了边数*2int n,m;struct node1{    int v,next,w;}edge[MAXM];int id;int head[MAXN];void init1(){    memset(head,-1,sizeof(head));    id=0;}void addedge(int u,int v,int w){    edge[id].v=v;    edge[id].w=w;    edge[id].next=head[u];    head[u]=id++;    edge[id].v=u;    edge[id].w=w;    edge[id].next=head[v];    head[v]=id++;}bool vis[MAXN];int minn[MAXN];//minn数组记录不同路径的最短路通过最少条路int dist[MAXN];void spfa(int s){    memset(vis,false,sizeof(vis));    memset(dist,INF,sizeof(dist));    memset(minn,INF,sizeof(minn));    vis[s]=true;    dist[s]=0;    minn[s]=0;    queue<int>que;    while(!que.empty()) que.pop();    que.push(s);    while(!que.empty())    {        int u=que.front();        que.pop();        vis[u]=false;        for(int i=head[u]; i!=-1; i=edge[i].next)        {            int v=edge[i].v;            if (dist[v]>dist[u]+edge[i].w)//找到更短的 更新            {                dist[v]=dist[u]+edge[i].w;                minn[v]=minn[u]+1;                if (!vis[v])                {                    vis[v]=true;                    que.push(v);                }            }            if (dist[v]==dist[u]+edge[i].w)//找到同样短的            {                minn[v]=min(minn[u]+1,minn[v]);                if (!vis[v])                {                    vis[v]=true;                    que.push(v);                }            }        }    }}//以上是用spfa找最短路的代码struct Edge{    int v, f;    int next;}Edge[MAXM];int cnt;int first[MAXN], level[MAXN];int q[MAXN];void init2(){    cnt = 0;    memset(first, -1, sizeof(first));}void add(int u, int v, int f){    Edge[cnt].v = v, Edge[cnt].f = f;    Edge[cnt].next = first[u], first[u] = cnt++;    Edge[cnt].v = u, Edge[cnt].f = 0;  //增加一条反向弧,容量为0    Edge[cnt].next = first[v], first[v] = cnt++;}void build()//将找到的最短路建图 flow为1{    for(int i = 1; i <= n; ++i) {        for(int j = head[i]; ~j; j = edge[j].next) {            int v = edge[j].v, w = edge[j].w;            if(dist[v] - dist[i] == w) {                add(i, v, 1);            }        }    }}int bfs(int s, int t) //构建层次网络{    memset(level, 0, sizeof(level));    level[s] = 1;    int front = 0, rear = 1;    q[front] = s;    while(front < rear)    {        int x = q[front++];        if(x == t) return 1;        for(int e = first[x]; e != -1; e = Edge[e].next)        {            int v = Edge[e].v, f = Edge[e].f;            if(!level[v] && f)            {                level[v] = level[x] + 1;                q[rear++] = v;            }        }    }    return 0;}int dfs(int u, int maxf, int t){    if(u == t) return maxf;    int ret = 0;    for(int e = first[u]; e != -1; e = Edge[e].next)    {        int v = Edge[e].v, f = Edge[e].f;        if(level[u] + 1 == level[v] && f)        {            int Min = min(maxf-ret, f);            f = dfs(v, Min, t);            Edge[e].f -= f;            Edge[e^1].f += f;            ret += f;            if(ret == maxf) return ret;        }    }    return ret;}int Dinic(int s, int t) //Dinic{    int ans = 0;    while(bfs(s, t)) ans += dfs(s, INF, t);    return ans;}//这些是dinic算最小割的算法int main(){    while(~scanf("%d%d",&n,&m))    {        init1();init2();        for(int i=1; i<=m; i++)        {            int u,v,w;            scanf("%d%d%d",&u,&v,&w);            addedge(u,v,w);        }        spfa(1);        build();        int ans=Dinic(1,n);        printf("%d %d\n", ans, m-minn[n]);        //最小割为答案一,总边数-最少边的最短路边数为答案二    }    return 0;}
0 0
原创粉丝点击