Marriage Match IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5253 Accepted Submission(s): 1559
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?
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
Author
starvae@HDU
Source
HDOJ Monthly Contest – 2010.06.05
#include <bits/stdc++.h>
#define P pair<int, int>
using namespace std;
const int inf = 0x7fffffff;
const int mn = 2000, mm = 200010;
int max_flow(int, int);
void addedge(int, int, int);
int n, m;
struct Node
{
int from, to, val;
} E[mm];
struct Edge
{
int to, nx, cap;
} edge1[mm], edge2[mm];
int Fr1[mn], Fr2[mn];
int dis1[mn], dis2[mn];
void dijkstra(int st, Edge edge[], int Fr[], int diss[])
{
priority_queue<P, vector<P>, greater<P> > que;
for (int i = 0; i <= n; i++)
diss[i] = inf;
diss[st] = 0;
que.push(P(0, st));
while (!que.empty())
{
P p = que.top();
que.pop();
if (diss[p.second] < p.first)
continue;
int v = p.second;
for (int i = Fr[v]; i != -1; i = edge[i].nx)
{
if (diss[edge[i].to] > diss[v] + edge[i].cap)
{
diss[edge[i].to] = diss[v] + edge[i].cap;
que.push(P(diss[edge[i].to], edge[i].to));
}
}
}
}
int edge;
int fr[mn];
int cur[mn]; // 当前弧优化
int lv[mn];
void init()
{
edge = 0;
memset(fr, -1, sizeof fr);
memset(Fr1, -1, sizeof Fr1);
memset(Fr2, -1, sizeof Fr2);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("D:\\in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int T;
scanf("%d", &T);
while (T--)
{
init();
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++)
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
E[i].from = a, E[i].to = b, E[i].val = c;
edge1[i].to = b, edge1[i].nx = Fr1[a], edge1[i].cap = c;
Fr1[a] = i;
edge2[i].to = a, edge2[i].nx = Fr2[b], edge2[i].cap = c;
Fr2[b] = i;
}
int A, B;
scanf("%d %d", &A, &B);
/// 两次dijkstra求出源点和汇点到每个点的最短路
dijkstra(A, edge1, Fr1, dis1);
dijkstra(B, edge2, Fr2, dis2);
int DIS = dis1[B];
for (int i = 1; i <= m; i++)
{
/// 如果源点到当前边的起点的距离+边长+终点到汇点的距离 = 源点到汇点的最短距离
/// 则当前边为从源点到汇点最短路中的一条边
if (dis1[E[i].from] + E[i].val + dis2[E[i].to] == DIS)
addedge(E[i].from, E[i].to, 1);
}
printf("%d\n", max_flow(A, B)); /// 求最大流
}
return 0;
}
struct node
{
int to, val, nx, fan;
} e[mm];
void addedge(int u, int v, int w)
{
edge++;
e[edge].to = v, e[edge].val = w, e[edge].nx = fr[u], e[edge].fan = edge + 1;
fr[u] = edge;
edge++;
e[edge].to = u, e[edge].val = 0, e[edge].nx = fr[v], e[edge].fan = edge - 1;
fr[v] = edge;
}
void bfs(int s)
{
memset(lv, 0, sizeof lv);
lv[s] = 1;
queue<int> q;
q.push(s);
while (!q.empty())
{
int t = q.front();
q.pop();
for (int i = fr[t]; i != -1; i = e[i].nx)
{
if (e[i].val > 0 && !lv[e[i].to])
{
lv[e[i].to] = lv[t] + 1;
q.push(e[i].to);
}
}
}
}
int dfs(int s, int t, int f)
{
if (s == t)
return f;
for (int &i = cur[s]; i != -1; i = e[i].nx)
{
if (e[i].val > 0 && lv[s] < lv[e[i].to])
{
int d = dfs(e[i].to, t, min(f, e[i].val));
if (d > 0)
{
e[i].val -= d;
e[e[i].fan].val += d;
return d;
}
}
}
return 0;
}
int max_flow(int s, int t)
{
int flow = 0;
while (1)
{
bfs(s);
if (!lv[t])
break;
for (int i = 0; i <= n; i++)
cur[i] = fr[i];
int f = 0;
while ((f = dfs(s, t, inf)) > 0)
flow += f;
}
return flow;
}