1030. Travel Plan (30)
来源:互联网 发布:美丽水世界 mac 绿屏 编辑:程序博客网 时间:2024/04/29 04:50
- Travel Plan (30)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
A traveler’s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:
City1 City2 Distance Cost
where the numbers are all integers no more than 500, and are separated by a space.
Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.
Sample Input
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output
0 2 3 3 40
熟悉了此类题型的思路后,本题便十分简单了。
#include<iostream>#include<vector>#include<stack>using namespace std;vector<int> c[505];int path[505];int load[505][505];int cost[505][505];#define INF 100000000typedef struct node{ int dist,w; bool visited; node(){ visited=false; dist=INF; w=0; }};node city[505];int n,m,c1,c2;int find(){ int find=-1,min=INF; for(int i=0;i<n;i++){ if(city[i].dist<min&&city[i].visited==false) { min=city[i].dist; find=i; } } return find;}int main(){ freopen("in.txt","r",stdin); while(scanf("%d%d%d%d",&n,&m,&c1,&c2)!=EOF) { for(int i=0;i<m;i++){ int t1,t2; cin>>t1>>t2; cin>>load[t2][t1]>>cost[t2][t1]; cost[t1][t2]=cost[t2][t1]; load[t1][t2]=load[t2][t1]; c[t1].push_back(t2); c[t2].push_back(t1); } city[c1].dist=0; city[c1].w=0; int v=find(); while(v!=-1){ city[v].visited=true; for(int i=0;i<c[v].size();i++) { int t=c[v][i]; if(city[t].visited==false){ if(city[t].dist>city[v].dist+load[v][t]) { city[t].dist=city[v].dist+load[v][t]; city[t].w=cost[v][t]+city[v].w; path[t]=v; } else if(city[t].dist==city[v].dist+load[v][t]) { if(city[t].w>cost[v][t]+city[v].w) { city[t].w=cost[v][t]+city[v].w; path[t]=v; } } } } v=find(); } int t=c2; stack<int> pout; pout.push(t); while(t!=c1) { t=path[t]; pout.push(t); } while(!pout.empty()) { printf("%d ",pout.top()); pout.pop(); } printf("%d %d",city[c2].dist,city[c2].w); } return 0;}
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 租房
- iOS并行程序开发- GCD & NSOperationQueue(1)
- 车牌识别停车计费系统性能表现二 计费系统功能强大
- SVN
- YII 组件 CActiveForm
- 1030. Travel Plan (30)
- 车牌识别计费系统 优越表现三 LED显示和语音播报
- Eclipse和MyEclipse快捷键大全
- git non fast forward rejected && [git]error: RPC failed; result=55, HTTP code = 0
- jQuery EasyUI使用教程之在数据网格中创建列组
- Android开发的那些坑和小技巧
- UI基础学习之(十四):手势
- 近日学习心得
- 车牌识别计费系统 优越表现四 高速智能道闸