Marriage Match IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4780 Accepted Submission(s): 1432
Problem Description
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?
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.
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题意:求最短路的个数思路:先用SPFA求出最短路径,然后用dinic算法求出最大网络流,即最短的个数,以最短路径上的边建图.代码:#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> #include <cmath> #include <queue> #include <vector> using namespace std; const int maxn=10005; const int maxm=300005; const int inf=0x3f3f3f3f; int head[maxn]; int Next[maxm]; int depth[maxn]; int dis[maxn]; int vis[maxn]; int m,n; int s,t; int cnt; struct edge { int u,v,w; }e[maxm]; void addedge(int u,int v,int w) { cnt++; Next[cnt]=head[u]; head[u]=cnt; e[cnt].u=u; e[cnt].v=v; e[cnt].w=w; cnt++; Next[cnt]=head[v]; head[v]=cnt; e[cnt].u=v; e[cnt].v=u; e[cnt].w=0; } struct node { int v,c; }; vector<node>E[1005]; int bfs() { queue<int>q; memset(depth,0,sizeof(depth)); depth[s]=1; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=Next[i]) { int v=e[i].v; if(depth[v]==0&&e[i].w>0) { depth[v]=depth[u]+1; q.push(v); } } } if(depth[t]==0) return 0; return 1; } int dfs(int u,int w) { if(u==t)return w; int f=0; for(int i=head[u];i!=-1;i=Next[i]) { int v=e[i].v; if(depth[v]==depth[u]+1&&e[i].w>0) { int d=dfs(v,min(w-f,e[i].w)); e[i].w-=d; e[i^1].w+=d; f+=d; if(f==w)break; } } if(!f)depth[u]=0; return f; } void spfa() { queue<int>Q; for(int i=0;i<=n;i++) { dis[i]=inf; vis[i]=0; } vis[s]=1; dis[s]=0; Q.push(s); while(!Q.empty()) { int u=Q.front(); Q.pop(); vis[u]=0; for(int i=0; i<E[u].size(); i++) { int v=E[u][i].v; if(dis[v]>dis[u]+E[u][i].c) { dis[v]=dis[u]+E[u][i].c; if(!vis[v]) { vis[v]=1; Q.push(v); } } } } } int main() { int T; scanf("%d",&T); while(T--) { int i; scanf("%d%d",&n,&m); for(i=1; i<=n; i++) E[i].clear(); int a,b,c; cnt=-1; memset(head,-1,sizeof(head)); node tmp; for(i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&c); if(a==b)continue; tmp.v=b; tmp.c=c; E[a].push_back(tmp); } scanf("%d%d",&s,&t); spfa(); for(i=1; i<=n; i++) for(int j=0; j<E[i].size(); j++) if(dis[E[i][j].v]==dis[i]+E[i][j].c) addedge(i,E[i][j].v,1); int ans=0; while(bfs()) { while(int d=dfs(s,inf)) { ans+=d; } } printf("%d\n",ans); } return 0; }