Marriage Match IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5008 Accepted Submission(s): 1498
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
题解:
找出所有最短路,建边,边权全部为1,求起点到终点的最大流。
代码:
#include<bits/stdc++.h>
using namespace std;
#define inf 0xf3f3f3f
const int maxn=1005;
struct node
{
int to,cost;
};
struct edge
{
int to,cap,rev;
};
vector<edge>p[maxn];
vector<node>G[maxn];
vector<int>rode[maxn];
int d[maxn],n,m,dist[maxn],iter[maxn],level[maxn];
bool vis[maxn];
void DJA(int s,int t)
{
memset(vis,0,sizeof(vis));
memset(d,inf,sizeof(d));
d[s]=0;
while(1)
{
int v=-1;
for(int i=1;i<=n;i++)
if(!vis[i]&&(v==-1||d[v]>d[i]))v=i;
if(v==-1)break;
vis[v]=1;
for(int i=0;i<G[v].size();i++)
{
node e=G[v][i];
if(vis[e.to])continue;
if(d[e.to]>d[v]+e.cost)
{
d[e.to]=d[v]+e.cost;
rode[e.to].clear();
rode[e.to].push_back(v);
}
else if(d[e.to]==d[v]+e.cost)
{
rode[e.to].push_back(v);
}
}
}
}
void add(int from,int to,int cap)
{
p[from].push_back((edge){to,cap,p[to].size()});
p[to].push_back((edge){from,0,p[from].size()-1});
}
void add_edge()
{
for(int i=1;i<=n;i++)
{
for(int j=0;j<rode[i].size();j++)
add(rode[i][j],i,1);
}
}
void bfs(int s)
{
memset(level,-1,sizeof(level));
queue<int>P;level[s]=0;P.push(s);
while(!P.empty())
{
int v=P.front();P.pop();
for(int i=0;i<p[v].size();i++)
{
edge e=p[v][i];
if(e.cap>0&&level[e.to]<0)
{
level[e.to]=level[v]+1;
P.push(e.to);
}
}
}
}
int dfs(int v,int t,int f)
{
if(v==t)return f;
for(int &i=iter[v];i<p[v].size();i++)
{
edge &e=p[v][i];
if(e.cap>0&&level[e.to]>level[v])
{
int d=dfs(e.to,t,min(f,e.cap));
if(d>0)
{
e.cap-=d;
p[e.to][e.rev].cap+=d;
return d;
}
}
}
return 0;
}
int max_flow(int s,int t)
{
add_edge();
int flow=0;
while(1)
{
bfs(s);
if(level[t]<0)return flow;
memset(iter,0,sizeof(iter));
int f;
while((f=dfs(s,t,inf))>0)flow+=f;
}
}
int main()
{
int T;scanf("%d",&T);
while(T--)
{
for(int i=0;i<maxn;i++)G[i].clear(),rode[i].clear(),p[i].clear();
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int x,y,z;scanf("%d%d%d",&x,&y,&z);
G[x].push_back((node){y,z});
}
int s,t;scanf("%d%d",&s,&t);
DJA(s,t);
printf("%d\n",max_flow(s,t));
}
return 0;
}