hdu5294 Tricks Device 最短路+最小割

Tricks Device

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


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

  无向图N个点M条边,输出最少删掉几条边破坏最短路,最多删掉几条边不破坏最短路。

  最多删几条边不破坏最短路的答案是M减去最短路里边数最少的那个,最少删掉几条边破坏最短路的做法是把所有在最短路上的边重新建图,变成流量为1的网络流,求最小割。

  在求最短路的时候记录下上一节点。

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<string>  
  4. #include<cstring>  
  5. #include<cmath>  
  6. #include<vector>  
  7. #include<queue>  
  8.   
  9. using namespace std;  
  10. typedef long long LL;  
  11.   
  12. const int INF=0x3f3f3f3f;  
  13. const int MAXN=2010;  
  14.   
  15. int N,M;  
  16.   
  17. struct Edge{  
  18.     int from,to,cap,flow;  
  19. };  
  20.   
  21. struct Dinic{  
  22.     int n,m,s,t;            //结点数,边数(包括反向弧),源点编号和汇点编号  
  23.     vector<Edge> edges;     //边表。edge[e]和edge[e^1]互为反向弧  
  24.     vector<int> G[MAXN];    //邻接表,G[i][j]表示节点i和第j条边在e数组中的序号  
  25.     bool vis[MAXN];         //BFS使用  
  26.     int d[MAXN];            //从起点到i的距离  
  27.     int cur[MAXN];          //当前弧下标  
  28.   
  29.     void clear_all(int n){  
  30.         for(int i=0;i<n;i++) G[i].clear();  
  31.         edges.clear();  
  32.     }  
  33.     void clear_flow(){  
  34.         int len=edges.size();  
  35.         for(int i=0;i<len;i++) edges[i].flow=0;  
  36.     }  
  37.     void add_edge(int from,int to,int cap){  
  38.         edges.push_back((Edge){from,to,cap,0});  
  39.         edges.push_back((Edge){to,from,0,0});  
  40.         m=edges.size();  
  41.         G[from].push_back(m-2);  
  42.         G[to].push_back(m-1);  
  43.     }  
  44.     bool BFS(){  
  45.         memset(vis,0,sizeof(vis));  
  46.         queue<int> q;  
  47.         q.push(s);  
  48.         d[s]=0;  
  49.         vis[s]=1;  
  50.         while(!q.empty()){  
  51.             int x=q.front();  
  52.             q.pop();  
  53.             int len=G[x].size();  
  54.             for(int i=0;i<len;i++){  
  55.                 Edge& e=edges[G[x][i]];  
  56.                 if(!vis[e.to]&&e.cap>e.flow){  
  57.                     vis[e.to]=1;  
  58.                     d[e.to]=d[x]+1;  
  59.                     q.push(e.to);  
  60.                 }  
  61.             }  
  62.         }  
  63.         return vis[t];  
  64.     }  
  65.     int DFS(int x,int a){  
  66.         if(x==t||a==0) return a;  
  67.         int flow=0,f,len=G[x].size();  
  68.         for(int& i=cur[x];i<len;i++){  
  69.             Edge& e=edges[G[x][i]];  
  70.             if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0){  
  71.                 e.flow+=f;  
  72.                 edges[G[x][i]^1].flow-=f;  
  73.                 flow+=f;  
  74.                 a-=f;  
  75.                 if(a==0) break;  
  76.             }  
  77.         }  
  78.         return flow;  
  79.     }  
  80.     int maxflow(int s,int t){  
  81.         this->s=s;  
  82.         this->t=t;  
  83.         int flow=0;  
  84.         while(BFS()){  
  85.             memset(cur,0,sizeof(cur));  
  86.             flow+=DFS(s,INF);  
  87.         }  
  88.         return flow;  
  89.     }  
  90.     int mincut(){   //call this after maxflow  
  91.         int ans=0;  
  92.         int len=edges.size();  
  93.         for(int i=0;i<len;i++){  
  94.             Edge& e=edges[i];  
  95.             if(vis[e.from]&&!vis[e.to]&&e.cap>0) ans++;  
  96.         }  
  97.         return ans;  
  98.     }  
  99.     void reduce(){  
  100.         int len=edges.size();  
  101.         for(int i=0;i<len;i++) edges[i].cap-=edges[i].flow;  
  102.     }  
  103. }solver;  
  104.   
  105. int d[MAXN];  
  106. vector<int> p[MAXN];  
  107.   
  108. struct HeapNode{  
  109.     int u,d;  
  110.     bool operator < (const HeapNode& rhs) const{  
  111.         return d>rhs.d;  
  112.     }  
  113. };  
  114. struct Edge2{  
  115.     int u,v,dist;  
  116. };  
  117.   
  118. struct Dijkstra{  
  119.     int n,m;  
  120.     vector<Edge2> edges;  
  121.     vector<int> G[MAXN];  
  122.     bool done[MAXN];  
  123.   
  124.     void init(int n){  
  125.         this->n=n;  
  126.         for(int i=0;i<n;i++) G[i].clear();  
  127.         edges.clear();  
  128.     }  
  129.     void add_edge(int u,int v,int dist){  
  130.         edges.push_back((Edge2){u,v,dist});  
  131.         m=edges.size();  
  132.         G[u].push_back(m-1);  
  133.     }  
  134.     void dijkstra(int s){  
  135.         priority_queue<HeapNode> q;  
  136.         for(int i=0;i<n;i++) p[i].clear();  
  137.         for(int i=0;i<n;i++) d[i]=INF;  
  138.         d[s]=0;  
  139.         memset(done,0,sizeof(done));  
  140.         q.push((HeapNode){s,0});  
  141.         while(!q.empty()){  
  142.             HeapNode x=q.top();  
  143.             q.pop();  
  144.             int u=x.u;  
  145.             if(done[u]) continue;  
  146.             done[u]=true;  
  147.             int L=G[u].size();  
  148.             for(int i=0;i<L;i++){  
  149.                 Edge2& e=edges[G[u][i]];  
  150.                 if(d[e.v]>d[u]+e.dist){  
  151.                     d[e.v]=d[u]+e.dist;  
  152.                     p[e.v].clear();  
  153.                     p[e.v].push_back(u);  
  154.                     q.push((HeapNode){e.v,d[e.v]});  
  155.                 }  
  156.                 else if(d[e.v]==d[u]+e.dist){  
  157.                     p[e.v].push_back(u);  
  158.                     q.push((HeapNode){e.v,d[e.v]});  
  159.                 }  
  160.             }  
  161.         }  
  162.     }  
  163. }solver2;  
  164.   
  165. int num[MAXN];  
  166.   
  167. int dfs(int u){  
  168.     if(u==0) return 0;  
  169.     if(num[u]!=INF) return num[u];  
  170.     int len=p[u].size();  
  171.     for(int i=0;i<len;i++){  
  172.         int fa=p[u][i];  
  173.         solver.add_edge(fa,u,1);  
  174.         solver.add_edge(u,fa,1);  
  175.         num[u]=min(num[u],dfs(fa)+1);  
  176.     }  
  177.     return num[u];  
  178. }  
  179.   
  180. int main(){  
  181.     freopen("in.txt","r",stdin);  
  182.     while(scanf("%d%d",&N,&M)!=EOF){  
  183.         solver2.init(N+1);  
  184.         int u,v,t;  
  185.         for(int i=0;i<M;i++){  
  186.             scanf("%d%d%d",&u,&v,&t);  
  187.             u--;  
  188.             v--;  
  189.             solver2.add_edge(u,v,t);  
  190.             solver2.add_edge(v,u,t);  
  191.         }  
  192.         solver2.dijkstra(0);  
  193.         memset(num,INF,sizeof(num));  
  194.         solver.clear_all(N+1);  
  195.         int n=dfs(N-1);  
  196.         solver.maxflow(0,N-1);  
  197.         int ans1=solver.mincut(),ans2=M-num[N-1];  
  198.         printf("%d %d\n",ans1,ans2);  
  199.     }  
  200.     return 0;  
  201. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值