hdu5294(最短路+最大流)

来源:互联网 发布:eve知乎 编辑:程序博客网 时间:2024/06/07 01:20

Tricks Device

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


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

Sample Output
2 6

题意:给定一个无向图,从起点到终点,只有走最短路!!!(这句话非常重要)才能在规定时限内到达,问最少去掉几条边使不能到达,最多去掉几条边仍能到达。

思路:由于是走最短路才能到达,首先的预处理就是求最短路!求出来最短路之后,把所有最短路的边添加到图里面,然后再在这个新建的图上进行操作,最少去掉几条边就是求边权值都相等的最大流,最多去掉几条边就是求边权值都相等的最短路!!这题的主要考点就是在最短路上面!!预处理非常重要。。。这题由于没搞清楚要走最短路wa哇傻了。。


#include <iostream>#include <stdio.h>#include <stdlib.h>#include<string.h>#include<algorithm>#include<math.h>#include<queue>using namespace std;typedef long long ll;const   int oo=1e9;/**oo 表示无穷大*/const  int mm=111111111;/**mm 表示边的最大数量,记住要是原图的两倍,在加边的时候都是双向的*/const  int mn=2010;/**mn 表示点的最大数量*/int node,src,dest,edge;/**node 表示节点数,src 表示源点,dest 表示汇点,edge 统计边数*/int ver[mm],flow[mm],nex[mm];/**ver 边指向的节点,flow 边的容量 ,next 链表的下一条边*/int head[mn],work[mn],dis[mn],q[mn];void prepare(int _node, int _src,int _dest){    node=_node,src=_src,dest=_dest;    for(int i=0; i<node; ++i)head[i]=-1;    edge=0;}/**增加一条 u 到 v 容量为 c 的边*/void addedge( int u,  int v,  int c){    ver[edge]=v,flow[edge]=c,nex[edge]=head[u],head[u]=edge++;    ver[edge]=u,flow[edge]=0,nex[edge]=head[v],head[v]=edge++;}/**广搜计算出每个点与源点的最短距离,如果不能到达汇点说明算法结束*/bool Dinic_bfs(){    int i,u,v,l,r=0;    for(i=0; i<node; ++i)dis[i]=-1;    dis[q[r++]=src]=0;    for(l=0; l<r; ++l)        for(i=head[u=q[l]]; i>=0; i=nex[i])            if(flow[i]&&dis[v=ver[i]]<0)            {                /**这条边必须有剩余容量*/                dis[q[r++]=v]=dis[u]+1;                if(v==dest)  return 1;            }    return 0;}/**寻找可行流的增广路算法,按节点的距离来找,加快速度*/int Dinic_dfs(  int u, int exp){    if(u==dest)  return exp;    /**work 是临时链表头,这里用 i 引用它,这样寻找过的边不再寻找*/    for(  int &i=work[u],v,tmp; i>=0; i=nex[i])        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)        {            flow[i]-=tmp;            flow[i^1]+=tmp;            /**正反向边容量改变*/            return tmp;        }    return 0;}int Dinic_flow(){    int i,ret=0,delta;    while(Dinic_bfs())    {        for(i=0; i<node; ++i)work[i]=head[i];        while((delta=Dinic_dfs(src,oo)))ret+=delta;    }    return ret;}int hea[mn],di[mn];bool vis[mn];struct data{    int to,next,w;} x[mn*mn];void spfa(int s,int n){    queue<int>q;    for (int i=0; i<=n; i++)        di[i]=oo;    memset(vis,0,sizeof(vis));    q.push(s);    di[s]=0;    while(!q.empty())    {        int h=q.front();        q.pop();        vis[h]=0;        for (int i=hea[h]; i!=-1; i=x[i].next)        {            int v=x[i].to;            int w=x[i].w;            if (di[v]>di[h]+w)            {                di[v]=di[h]+w;                if (!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }}int ip=0;void add(int u,int v,int w){    x[ip].to=v,x[ip].w=w,x[ip].next=hea[u],hea[u]=ip++;}int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        ip=0;        memset(hea,-1,sizeof(hea));        for(int i=0; i<m; i++)        {            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            add(a-1,b-1,c);            add(b-1,a-1,c);        }        spfa(0,n);        prepare(n,0,n-1);        for(int i=0; i<n; i++)            for(int j=hea[i]; j!=-1; j=x[j].next)                if(di[i]+x[j].w==di[x[j].to])                    addedge(i,x[j].to,1),x[j].w=1;        spfa(0,n);        printf("%d %d\n",Dinic_flow(),m-di[n-1]);    }}


0 0