[HDU 1142] A Walk Through the Forest
Problem Description
Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer, his office is on one side of a forest, and his house is on the other. A nice walk through the forest, seeing the birds and chipmunks is quite enjoyable.
The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house. He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take.
The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house. He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take.
大意:
给一张无向图,n 个点和 m 条边,cyb 在 1 号点,他要去 2 号点,
可以从 a 走到 b,当且仅当a到2的最短路,比b 到2的最短路长。
求 路径方案数
两条路径不同,当且仅当将两条路径中依次经过的边的编号不完全相同,
图可能会有重边;
Input
Input contains several test cases followed by a line
containing 0. Jimmy has numbered each intersection or joining of paths starting
with 1. His office is numbered 1, and his house is numbered 2. The first line of
each test case gives the number of intersections N, 1 < N ≤ 1000, and the
number of paths M. The following M lines each contain a pair of intersections a
b and an integer distance 1 ≤ d ≤ 1000000 indicating a path of length d between
intersection a and a different intersection b. Jimmy may walk a path any
direction he chooses. There is at most one path between any pair of
intersections.
Output
For each test case, output a single integer indicating
the number of different routes through the forest. You may assume that this
number does not exceed 2147483647
Sample Input
5 6
1 3 2
1 4 2
3 4 3
1 5 12
4 2 34
5 2 24
7 8
1 3 1
1 4 1
3 7 1
7 4 1
7 5 1
6 7 1
5 2 1
6 2 1
0
Sample Output
2
4
预处理出所有点到2的距离
第二次令f[i]是i点的方案数,f[v]+=f[u]
所以再来一次2开始的bfs,这次只有满足dist[u]<dist[v]才转移
注意要拓扑序,否则会出现dist[v]还没算完就入队
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 typedef long long lol; 7 struct Node 8 { 9 int next,to,dis; 10 }edge[200001]; 11 int head[100001],num,q[1000001],dist[100001],n,m,in[100001]; 12 lol ans[100001]; 13 bool vis[100001]; 14 void add(int u,int v,int d) 15 { 16 num++; 17 edge[num].next=head[u]; 18 head[u]=num; 19 edge[num].to=v; 20 edge[num].dis=d; 21 } 22 void spfa() 23 {int h,t,i; 24 q[1]=2; 25 h=0;t=1; 26 memset(dist,127/3,sizeof(dist)); 27 dist[2]=0; 28 while (h<t) 29 { 30 h++; 31 h%=1000000; 32 int u=q[h]; 33 vis[u]=0; 34 for (i=head[u];i;i=edge[i].next) 35 { 36 int v=edge[i].to; 37 if (dist[v]>dist[u]+edge[i].dis) 38 { 39 dist[v]=dist[u]+edge[i].dis; 40 if (vis[v]==0) 41 { 42 t++; 43 t%=1000000; 44 q[t]=v; 45 vis[v]=1; 46 } 47 } 48 } 49 } 50 } 51 int main() 52 {int i,j,u,v,d,h,t; 53 //freopen("mod.in","r",stdin); 54 //freopen("mod.out","w",stdout); 55 while (cin>>n>>m&&n&&m) 56 {num=0; 57 memset(head,0,sizeof(head)); 58 memset(edge,0,sizeof(edge)); 59 memset(ans,0,sizeof(ans)); 60 for (i=1;i<=m;i++) 61 { 62 scanf("%d%d%d",&u,&v,&d); 63 add(u,v,d); 64 add(v,u,d); 65 } 66 spfa(); 67 for (i=1;i<=n;i++) 68 for (j=head[i];j;j=edge[j].next) 69 if (dist[edge[j].to]>dist[i]) 70 in[edge[j].to]++; 71 // for (i=1;i<=n;i++) 72 // cout<<dist[i]<<endl; 73 q[1]=2; 74 h=0;t=1; 75 memset(vis,0,sizeof(vis)); 76 ans[2]=1; 77 while (h<t) 78 { 79 h++; 80 h%=1000000; 81 int u=q[h]; 82 for (i=head[u];i;i=edge[i].next) 83 { 84 int v=edge[i].to; 85 if (dist[v]>dist[u]) 86 {in[v]--; 87 ans[v]=(ans[v]+ans[u]); 88 if (in[v]==0) 89 { 90 t++; 91 t%=1000000; 92 q[t]=v; 93 } 94 } 95 96 } 97 } 98 cout<<ans[1]<<endl; 99 } 100 }