hdu5294Tricks Device【最短路+网络流】

来源:互联网 发布:售电软件报价 编辑:程序博客网 时间:2024/05/29 18:00

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
 

我就说这个题暑假见过,还以为是当时没好好做稀里糊涂过去的,结果是多校orz

题意:从1开始到n结束,给出双向道路,问至少去掉多少边最短路不是原始最短路长度,至多去掉多少条边最短路长度不变

很明显是最短路+网络流的题,关键是怎么结合,一直困扰我的是第一问怎么记录路径,前辈的博客很巧妙的分别利用1与n作为源点 ,分别跑一遍最短路,这个时候两个dist数组记录的就是以1、n为源点的单源最短路长度,理论上他俩无缝接起来都是最短路的方案(理论上是,实际上也是:P  )那么我就依次验证给定的边长是否在最短路上,在的话,给这对点建网络流的边(这个题最最讨厌的地方在于函数名,数组名都差不多,特别容易写错)流量是题中给定的第三个数组,求完最短路还要给这对点再建一遍最短路的边,边长为一,这样跑下来dist[n]就是最短路的最短长度了

#include <iostream>#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;const int inf=0x3f3f;const int maxn=2111;int n,m;struct Edge{    int v,cost;    Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}};vector<Edge>E[maxn];void addedge(int u,int v,int w){    E[u].push_back(Edge(v,w));}bool vis[maxn];int cnt[maxn];int dist[maxn],dist1[maxn];bool spfa(int start,int n){    memset(vis,false,sizeof(vis));    memset(dist,inf,sizeof(dist));    vis[start]=true;    dist[start]=0;    queue<int>que;    while(!que.empty()) que.pop();    que.push(start);    memset(cnt,0,sizeof(cnt));    cnt[start]=1;    while(!que.empty())    {        int u=que.front();        que.pop();        vis[u]=false;        for(int i=0;i<E[u].size();i++)        {            int v=E[u][i].v;            if(dist[v]>dist[u]+E[u][i].cost)            {                dist[v]=dist[u]+E[u][i].cost;                if(!vis[v])                {                    vis[v]=true;                    que.push(v);                    if(++cnt[v]>n) return false;                }            }        }    }    return true;}int w[60009],u[60009],v[60009];////////////////////////////////////////const int mm=161111;const int mn=600009;const int oo=1000000000;int node,src,dest,edge;int reach[mm],flow[mm],nxt[mm];int head[mn],work[mn],dis[mn],q[mn];inline int min(int a,int b){    return a<b?a:b;}inline 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;}inline void addedge(int u,int v,int c1,int c2){    reach[edge]=v,flow[edge]=c1,nxt[edge]=head[u],head[u]=edge++;    reach[edge]=u,flow[edge]=c2,nxt[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=nxt[i])            if(flow[i]&&dis[v=reach[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;    for(int &i=work[u],v,tmp;i>=0;i=nxt[i])        if(flow[i]&&dis[v=reach[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)        {            flow[i]-=tmp;            flow[i^1]+=tmp;            return tmp;        }dis[u]--;    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 bb[mn][2],dist2[mn];int main(){   // freopen("cin.txt","r",stdin);    while(~scanf("%d%d",&n,&m))    {        for(int i=0;i<=n;i++) E[i].clear();        for(int i=0;i<m;i++)        {            scanf("%d%d%d",&u[i],&v[i],&w[i]);            addedge(u[i],v[i],w[i]);            addedge(v[i],u[i],w[i]);        }        spfa(1,n);        memcpy(dist2,dist,sizeof(dist));        spfa(n,n);        int k=0;        for(int i=0;i<m;i++)        {            if(dist2[u[i]]>dist2[v[i]])swap(u[i],v[i]);            if(dist[v[i]]+w[i]+dist2[u[i]]==dist2[n])            {                bb[k][0]=u[i];                bb[k++][1]=v[i];            }        }        prepare(n+1,1,n);        for(int i=0;i<k;i++)        {            addedge(bb[i][0],bb[i][1],1,0);           // addedge(bb[i][1],bb[i][0],1);        }        int x=Dinic_flow();        for(int i=0;i<=n;i++) E[i].clear();        for(int i=0;i<k;i++)        {            addedge(bb[i][0],bb[i][1],1);            addedge(bb[i][1],bb[i][0],1);        }        spfa(1,n);        printf("%d %d\n",x,m-dist[n]);    }    return 0;}


0 0
原创粉丝点击