Marriage Match IV (hdu 3416 網路流+spfa最短路),hduspfa
Marriage Match IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2275 Accepted Submission(s): 691
Problem DescriptionDo 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?
InputThe 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.
OutputOutput a line with a integer, means the chances starvae can get at most.
Sample Input
37 81 2 11 3 12 4 13 4 14 5 14 6 15 7 16 7 11 76 71 2 12 3 11 3 33 4 13 5 14 6 15 6 11 62 21 2 11 2 21 2
Sample Output
211
Authorstarvae@HDU
SourceHDOJ Monthly Contest – 2010.06.05
Recommendlcy | We have carefully selected several similar problems for you: 3376 3491 1569 3418 3422
題意:有n個城市m條邊,告訴起點和終點,問從起點到終點不走重複邊的最短路有多少條。
思路:求最短路的條數可以用最大流,建圖時要去掉不是最短路上的邊,判斷某條邊是不是最短路上的邊:如果滿足dist1[u] + dist2[v] + E1[i].len = dist1[End]則可以說明該邊是最短路上的邊,其中 dist1[] 為各點到起點的最短距離,dist2[] 為各點到終點的最短距離。
代碼:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define MAXN 1005#define MAXM 300005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b) for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b) for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v) memset ((t) , v, sizeof(t))#define sf(n) scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf printf#define DBG pf("Hi\n")typedef long long ll;using namespace std;struct Edge{ int to,next,cap,flow;}edge[MAXM];int n,m,start,End;int tol,tol1,tol2;int head[MAXN];int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];struct EDGE{ int u,v,len,next;}E1[MAXM],E2[MAXM];int head1[MAXN],dist1[MAXN],inq[MAXN];int head2[MAXN],dist2[MAXN];void init(){ tol=0;tol1=0;tol2=0; memset(head,-1,sizeof(head)); memset(head1,-1,sizeof(head1)); memset(head2,-1,sizeof(head2));}void add1(int u,int v,int w){ E1[tol1].u=u; E1[tol1].v=v; E1[tol1].len=w; E1[tol1].next=head1[u]; head1[u]=tol1++;}void add2(int u,int v,int w){ E2[tol2].u=u; E2[tol2].v=v; E2[tol2].len=w; E2[tol2].next=head2[u]; head2[u]=tol2++;}//加邊,單向圖三個參數,雙向圖四個參數void addedge(int u,int v,int w,int rw=0){ edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u]; edge[tol].flow=0; head[u]=tol++; edge[tol].to=u; edge[tol].cap=rw; edge[tol].next=head[v]; edge[tol].flow=0; head[v]=tol++;}void spfa(int s,int *dist,int *head,EDGE *E) //spfa求最短路{ memset(inq,0,sizeof(inq)); queue<int>Q; Q.push(s); inq[s]=1; dist[s]=0; while (!Q.empty()) { int u=Q.front(); Q.pop(); inq[u]=0; for (int i=head[u];i+1;i=E[i].next) { int v=E[i].v; if (dist[v]>dist[u]+E[i].len) { dist[v]=dist[u]+E[i].len; if (!inq[v]) { inq[v]=1; Q.push(v); } } } }}//輸入參數:起點,終點,點的總數//點的編號沒有影響,只要輸入焦點的總數int sap(int start,int End,int N){ memset(gap,0,sizeof(gap)); memset(dep,0,sizeof(dep)); memcpy(cur,head,sizeof(head)); int u=start; pre[u]=-1; gap[0]=N; int ans=0; while (dep[start]<N) { if (u==End) { int Min=INF; for (int i=pre[u];i!=-1;i=pre[edge[i^1].to]) if (Min>edge[i].cap-edge[i].flow) Min=edge[i].cap-edge[i].flow; for (int i=pre[u];i!=-1;i=pre[edge[i^1].to]) { edge[i].flow+=Min; edge[i^1].flow-=Min; } u=start; ans+=Min; continue; } bool flag=false; int v; for (int i=cur[u];i!=-1;i=edge[i].next) { v=edge[i].to; if (edge[i].cap-edge[i].flow && dep[v]+1==dep[u]) { flag=true; cur[u]=pre[v]=i; break; } } if (flag) { u=v; continue; } int Min=N; for (int i=head[u];i!=-1;i=edge[i].next) if (edge[i].cap-edge[i].flow && dep[edge[i].to]<Min) { Min=dep[edge[i].to]; cur[u]=i; } gap[dep[u]]--; if (!gap[dep[u]]) return ans; dep[u]=Min+1; gap[dep[u]]++; if (u!=start) u=edge[pre[u]^1].to; } return ans;}int main(){ int i,j,t,u,v,w; scanf("%d",&t); while (t--) { init(); scanf("%d%d",&n,&m); for (i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); if (u==v) continue; //去自環 add1(u,v,w); add2(v,u,w); } scanf("%d%d",&start,&End); memset(dist1,INF,sizeof(dist1)); memset(dist2,INF,sizeof(dist2)); spfa(start,dist1,head1,E1); spfa(End,dist2,head2,E2); for (i=0;i<tol1;i++) //建圖 { if (dist1[E1[i].u]+dist2[E1[i].v]+E1[i].len==dist1[End]) addedge(E1[i].u,E1[i].v,1); } printf("%d\n",sap(start,End,n)); } return 0;}