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 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
[email protected]
Source
HDOJ Monthly Contest – 2010.06.05
Recommend
lcy | 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; }