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.
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
题目大意
假设$A$和$B$是相连的,当前在$A$处,如果$A$到终点的距离大于$B$到终点的距离,则可以从$A$通往$B$处,问满足这种的条件的路径条数。
题解
单源最短路+$DP$
首先求出终点到各个点的最短路$d[u]$。按照题意,需要$d[A]>d[B]$。重新构图,当且仅当$d[A]>d[B]$时加入有向边$A→B$。题目转化为了求该图中起点到终点的路径条数。
由于该图是$DAG$,可以用$DP$求解。
1 #include<map> 2 #include<set> 3 #include<cmath> 4 #include<ctime> 5 #include<queue> 6 #include<stack> 7 #include<vector> 8 #include<cstdio> 9 #include<string> 10 #include<cstdlib> 11 #include<cstring> 12 #include<iostream> 13 #include<algorithm> 14 #define LL long long 15 #define RE register 16 #define IL inline 17 using namespace std; 18 const int N=50000; 19 20 int n,m; 21 int u,v,c; 22 struct tt 23 { 24 int to,next,cost; 25 }edge[4*N+5]; 26 int path[N+5],top; 27 IL void Add(int u,int v,int c); 28 29 LL dist[N+5]; 30 IL void SPFA(); 31 32 struct G 33 { 34 int to,next; 35 }graph[N*2+5]; 36 int path_g[N+5],top_g; 37 int in[N+5]; 38 IL void Add_g(int u,int v); 39 40 LL ans[N+5]; 41 IL void DP(); 42 43 int main() 44 { 45 while(scanf("%d%d",&n,&m)==2) 46 { 47 top=top_g=0; 48 memset(path,0,sizeof(path)); 49 memset(path_g,0,sizeof(path_g)); 50 memset(ans,0,sizeof(ans)); 51 for (RE int i=1;i<=m;i++) 52 { 53 scanf("%d%d%d",&u,&v,&c); 54 Add(u,v,c); 55 Add(v,u,c); 56 } 57 SPFA(); 58 for (RE int i=1;i<=n;i++) for (RE int j=path[i];j;j=edge[j].next) if (dist[i]>dist[edge[j].to]) Add_g(i,edge[j].to); 59 DP(); 60 printf("%lld ",ans[2]); 61 } 62 return 0; 63 } 64 65 IL void Add(int u,int v,int c) 66 { 67 edge[++top].cost=c; 68 edge[top].next=path[u]; 69 edge[top].to=v; 70 path[u]=top; 71 } 72 IL void SPFA() 73 { 74 bool vis[N+5]={0}; 75 queue<int>Q; 76 while (!Q.empty()) Q.pop(); 77 Q.push(2); 78 memset(dist,127/3,sizeof(dist)); 79 dist[2]=0;vis[2]=1; 80 while (!Q.empty()) 81 { 82 int u=Q.front();Q.pop();vis[u]=0; 83 for (RE int i=path[u];i;i=edge[i].next) 84 { 85 int v=edge[i].to; 86 if (dist[v]>dist[u]+edge[i].cost) 87 { 88 dist[v]=dist[u]+edge[i].cost; 89 if (!vis[v]) 90 { 91 vis[v]=1; 92 Q.push(v); 93 } 94 } 95 } 96 } 97 } 98 IL void Add_g(int u,int v) 99 { 100 graph[++top_g].to=v; 101 graph[top_g].next=path_g[u]; 102 path_g[u]=top_g; 103 in[v]++; 104 } 105 IL void DP() 106 { 107 queue<int>Q; 108 while (!Q.empty()) Q.pop(); 109 ans[1]=1; 110 for (RE int i=1;i<=n;i++) if (!in[i]) Q.push(i); 111 while (!Q.empty()) 112 { 113 int u=Q.front();Q.pop(); 114 for (RE int i=path_g[u];i;i=graph[i].next) 115 { 116 int v=graph[i].to; 117 ans[v]=(ans[u]+ans[v]); 118 in[v]--; 119 if (!in[v]) Q.push(v); 120 } 121 } 122 }