Marriage Match IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4279 Accepted Submission(s): 1280
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
Author
starvae@HDU
Source
题意:
求出最短路有几条,途径不能重复走。
POINT:
主体网络流,先从ss来最短路,算出ss到各个点的长度。
之后可以判断每条边是否属于最短路,把这些边建立网络流,接下来就是Dinic。
代码很长,写了很久。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
const int maxn=1010;
const int inf=0x3f3f3f3f;
int dis[maxn],ss,tt;
int n,m;
struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int w,int f):
from(u),to(v),cap(w),flow(f){}
};
struct edge1
{
int from,to,w;
edge1(int u,int v,int ww):
from(u),to(v),w(ww){}
};
vector<edge1>edge1s;
vector<int>G1[maxn];
vector<Edge> edges;
vector<int>G[maxn];
void add(int u,int v,int w)
{
edges.push_back(Edge(u, v, 1, 0));
edges.push_back(Edge(v,u,0,0));
int m=edges.size();
G[u].push_back(m-2);
G[v].push_back(m-1);
}
void add1(int u,int v,int w)
{
edge1s.push_back(edge1(u,v,w));
int m=edge1s.size();
G1[u].push_back(m-1);
}
void spfa()
{
int vis[maxn];
memset(vis,0,sizeof vis);
memset(dis,inf,sizeof dis);
dis[ss]=0;
queue<int> q;
q.push(ss);;
while(!q.empty())
{
int now=q.front();q.pop();
vis[now]=0;
for(int i=0;i<G1[now].size();i++)
{
edge1 e=edge1s[G1[now][i]];
if(dis[e.to]>dis[e.from]+e.w)
{
dis[e.to]=dis[e.from]+e.w;
if(!vis[e.to])
{
q.push(e.to);
vis[e.to]=1;
}
}
}
}
}
void init()
{
for(int i=0;i<maxn;i++)
{
G1[i].clear();
G[i].clear();
}
edge1s.clear();
edges.clear();
}
int d[maxn];
int bfs()
{
int vis[maxn];
memset(vis,0,sizeof vis);
memset(d,0,sizeof d);
queue<int>q;
q.push(ss);
vis[ss]=1;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=0;i<G[x].size();i++)
{
Edge len=edges[G[x][i]];
if(len.cap>len.flow&&!vis[len.to])
{
d[len.to]=d[x]+1;
q.push(len.to);
vis[len.to]=1;
}
}
}
return vis[tt];
}
int cur[maxn];
int dfs(int x,int a)
{
if(x==tt||a==0) return a;
int flow=0,f=0;
for(int &i=cur[x];i<G[x].size();i++)
{
Edge &len=edges[G[x][i]];
if(d[x]+1==d[len.to]&&(f=dfs(len.to,min(a,len.cap-len.flow)))>0)
{
a-=f;
edges[G[x][i]].flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
}
if(a==0) break;
}
if(flow==0) d[x]=-1;
return flow;
}
int maxflow()
{
int ans=0;
while(bfs())
{
memset(cur,0,sizeof cur);
ans+=dfs(ss,inf);
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d %d",&n,&m);
while(m--)
{
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
if(u==v) continue;
add1(u,v,w);
}
scanf("%d %d",&ss,&tt);
spfa();
for(int i=1;i<=n;i++)
{
for(int j=0;j<G1[i].size();j++)
{
edge1 e=edge1s[G1[i][j]];
if(dis[e.to]==dis[e.from]+e.w)
{
add(e.from,e.to,1);
}
}
}
printf("%d\n",maxflow());
}
}