uva 10537/The Toll! Revisited
2016-10-05 13:35
351 查看
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.
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
题意:求1到2的路径条数,条件:如果a到b能走那么必须是a到2的最短距离大于b到2的最短距离。
代码:
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.
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
题意:求1到2的路径条数,条件:如果a到b能走那么必须是a到2的最短距离大于b到2的最短距离。
代码:
<span style="font-size:14px;">#include <iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<vector> #include<queue> #define big 999999999 using namespace std; struct node{ int x,l; }; struct pnode{ int x,d; bool operator < (const pnode &a) const{ return a.d<d; } }; int n,m,d[1005],vis[1005],dp[1005]; vector<node>g[1005]; void dijsk() { priority_queue<pnode>q; m 4000 emset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) d[i]=big; d[2]=0; q.push((pnode){2,0}); while(!q.empty()) { pnode v=q.top(); q.pop(); //d[v.x]=v.d;//不能在这里做此操作,想一想并知。 if(vis[v.x]) continue; vis[v.x]=1; int l=g[v.x].size(); for(int i=0;i<l;i++){ int j=g[v.x][i].x; if(d[j]>d[v.x]+g[v.x][i].l){ d[j]=d[v.x]+g[v.x][i].l; q.push((pnode){j,d[j]}); } } } } int dfs(int s) { if(dp[s]) return dp[s];//不用dp数组存会超时。 if(s==2) return 1; int r=0,l=g[s].size(); for(int i=0;i<l;i++) { int j=g[s][i].x; if(d[j]<d[s]) { r+=dfs(j); } } dp[s]=r; return r; } int main() { int u,v,l; while(scanf("%d",&n)&&n) { for(int i=1;i<=n;i++) g[i].clear(); scanf("%d",&m); for(int i=1;i<=m;i++){ scanf("%d %d %d",&v,&u,&l); g[v].push_back((node){u,l}); g[u].push_back((node){v,l}); } dijsk(); memset(dp,0,sizeof(dp)); cout<<dfs(1)<<endl; } return 0; } </span>
相关文章推荐
- UVA 10537 The Toll! Revisited(dijkstra输出字典序最小的路径)
- UVA 10537 The Toll! Revisited 过路费(最短路,经典变形)
- UVA 10537 - The Toll! Revisited(dijstra扩展)
- UVA10537[The Toll! Revisited] dijkstra/spfa 反向建图
- 【UVA10537】The Toll! Revisited (逆推最短路)
- UVa 10537 The Toll! Revisited! Dijkstra最短路
- UVa - 10537 - The Toll! Revisited(单源最短路)
- UVa10537 - The Toll! Revisited(dijkstra+二分)
- uva 10537 The Toll! Revisited
- UVa 10537 The Toll! Revisited (最短路)
- UVA 10537 The Toll! Revisited(最短路变形+输出字典序最小路径)
- Uva 10537 The Toll! Revisited
- UVa 10537 - The Toll! Revisited
- uva 10537 - The Toll! Revisited (逆向思维+最短路+输出字典序最小路径)
- UVA 10537 The Toll! Revisited(最短路变形)
- 【UVA10537】The Toll! Revisited
- uva 10537 - The Toll! Revisited(最短路)
- UVA 10537 - The Toll! Revisited(dijstra扩张)
- uva 10537 - The Toll! Revisited (逆向思维最短路)
- UVA - 10537 The Toll! Revisited (最短路变形逆推)