HDU 3416 Marriage Match IV
2015-11-14 20:19
337 查看
Marriage Match IV
Time Limit: 1000msMemory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3416
64-bit integer IO format: %I64d Java class name: Main
Do not sincere non-interference。
Like that show, now starvae also take part in a show, but it take place between city A and B. Starvae is in city A and girls are in city B. Every time starvae can get to city B and make a data with a girl he likes. But there are two problems with it, one is starvae must get to B within least time, it's said that he must take a shortest path. Other is no road can be taken more than once. While the city starvae passed away can been taken more than once.
So, under a good RP, starvae may have many chances to get to city B. But he don't know how many chances at most he can make a data with the girl he likes . Could you help starvae?
Input
The first line is an integer T indicating the case number.(1<=T<=65)For each case,there are two integer n and m in the first line ( 2<=n<=1000, 0<=m<=100000 ) ,n is the number of the city and m is the number of the roads.
Then follows m line ,each line have three integers a,b,c,(1<=a,b<=n,0<c<=1000)it means there is a road from a to b and it's distance is c, while there may have no road from b to a. There may have a road from a to a,but you can ignore it. If there are two roads from a to b, they are different.
At last is a line with two integer A and B(1<=A,B<=N,A!=B), means the number of city A and city B.
There may be some blank line between each case.
Output
Output a line with a integer, means the chances starvae can get at most.Sample Input
3 7 8 1 2 1 1 3 1 2 4 1 3 4 1 4 5 1 4 6 1 5 7 1 6 7 1 1 7 6 7 1 2 1 2 3 1 1 3 3 3 4 1 3 5 1 4 6 1 5 6 1 1 6 2 2 1 2 1 1 2 2 1 2
Sample Output
2 1 1
Source
HDOJ Monthly Contest – 2010.06.05解题:直接把所有的最短路拿出来跑最小割
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 1010; struct arc{ int to,w,next; arc(int x = 0,int y = 0,int z = -1){ to = x; w = y; next = z; } }e[1000005]; int head[maxn],hd[maxn],d[maxn],gap[maxn],tot,n,m,S,T; bool in[maxn] = {}; void add(int head[maxn],int u,int v,int wa,int wb){ e[tot] = arc(v,wa,head[u]); head[u] = tot++; e[tot] = arc(u,wb,head[v]); head[v] = tot++; } void spfa(){ queue<int>q; memset(d,0x3f,sizeof d); d[S] = 0; q.push(S); while(!q.empty()){ int u = q.front(); q.pop(); in[u] = false; for(int i = hd[u]; ~i; i = e[i].next){ if(d[e[i].to] > d[u] + e[i].w){ d[e[i].to] = d[u] + e[i].w; if(!in[e[i].to]){ in[e[i].to] = true; q.push(e[i].to); } } } } for(int i = 1; i <= n; ++i){ for(int j = hd[i]; ~j; j = e[j].next){ if(d[e[j].to] == d[i] + e[j].w) add(head,i,e[j].to,1,0); } } } int dfs(int u,int low){ if(u == T) return low; int tmp = 0,minH = n - 1; for(int i = head[u]; ~i; i = e[i].next){ if(e[i].w){ if(d[e[i].to] + 1 == d[u]){ int a = dfs(e[i].to,min(low,e[i].w)); e[i].w -= a; e[i^1].w += a; tmp += a; low -= a; if(!low) break; if(d[S] >= n) return tmp; } if(e[i].w) minH = min(minH,d[e[i].to]); } } if(!tmp){ if(--gap[d[u]] == 0) d[S] = n; ++gap[d[u] = minH + 1]; } return tmp; } void bfs(){ queue<int>q; memset(d,-1,sizeof d); memset(gap,0,sizeof gap); q.push(T); d[T] = 0; while(!q.empty()){ int u = q.front(); q.pop(); ++gap[d[u]]; for(int i = head[u]; ~i; i = e[i].next){ if(d[e[i].to] == -1){ d[e[i].to] = d[u] + 1; q.push(e[i].to); } } } } int sap(int ret = 0){ bfs(); while(d[S] < n) ret += dfs(S,INF); return ret; } int main(){ int kase,u,v,w; scanf("%d",&kase); while(kase--){ scanf("%d%d",&n,&m); memset(head,-1,sizeof head); memset(hd,-1,sizeof hd); for(int i = tot = 0; i < m; ++i){ scanf("%d%d%d",&u,&v,&w); if(u == v) continue; add(hd,u,v,w,INF); } scanf("%d%d",&S,&T); spfa(); printf("%d\n",sap()); } return 0; }
View Code
相关文章推荐
- 217 Contains Duplicate
- 226 Invert Binary Tree
- 1010. Radix (25)
- Hadoop分布式缓存(DistributedCache)
- 59. 预定义超全局变量
- 压缩感知中的lp球:p范数最优化为什么总会导致一个稀疏的解的原因
- 【转】熵、信息增益以及其他
- 《大道至简》第七章第八章读后感
- jemalloc/jemalloc.h: No such file or directory
- CentOS下MySQL 5.7.9编译安装步骤详解
- 开篇
- 283 Move Zeroes
- 【转】C++ 智能指针详解
- iOS9的网络请求简述
- 无法解析的外部符号解决方法汇总
- python import UMD has deleted
- CALayer
- 100 Same Tree
- Android Shellcode Telnetd with Parameters
- eclipse工具栏不显示sdk和adb图标?