1030. Travel Plan
来源:互联网 发布:福田网络布线 编辑:程序博客网 时间:2024/06/05 11:26
1030. Travel Plan (30)
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 Input4 5 0 30 1 1 201 3 2 300 3 4 100 2 2 202 3 1 20Sample Output
0 2 3 3 40
题解:
#include <iostream>#include <vector>#include <algorithm>#include <deque>using namespace std;#define maxlen 10000000class road{public:int city;int length;int cost;};class city{public:vector<road> aroad;int path;int allcost;bool finded;int father_city;city():path(maxlen),finded(false),allcost(maxlen),father_city(maxlen){}};bool cmp(road a, road b){if(a.length==b.length)return a.cost<b.cost;return a.length<b.length;}void shortest(deque<int> &que,vector<city> &acity,int c_end){int now=que[0];int i;for(i=0;i<acity[now].aroad.size();i++)//分析与que[0]相邻的点{road adj_city=acity[now].aroad[i];if(acity[adj_city.city].finded)continue;que.push_back(adj_city.city);if(acity[adj_city.city].path>acity[now].path+adj_city.length||(acity[adj_city.city].path==acity[now].path+adj_city.length&&acity[adj_city.city].allcost>acity[now].allcost+adj_city.cost))//修改花费{acity[adj_city.city].path=acity[now].path+adj_city.length;acity[adj_city.city].allcost=acity[now].allcost+adj_city.cost;acity[adj_city.city].father_city=now;//父城市修改}}acity[now].finded=true;que.pop_front();if(que.empty()||acity[c_end].finded)//已经遍历所有的,或者到达目标点return;shortest(que,acity,c_end);}int main(){int n,m;//n cities and m roadsint c_begin,c_end;cin>>n>>m>>c_begin>>c_end;vector<city> acity(n);int i,tmp1,tmp2,tmpdis,tmpcost;road tmp;for(i=0;i<m;i++){cin>>tmp1>>tmp2>>tmpdis>>tmpcost;//City1 City2 Distance Costtmp.city=tmp2;tmp.length=tmpdis;tmp.cost=tmpcost;acity[tmp1].aroad.push_back(tmp);tmp.city=tmp1;acity[tmp2].aroad.push_back(tmp);}for(i=0;i<n;i++)sort(acity[i].aroad.begin(),acity[i].aroad.end(),cmp);deque<int> que;acity[c_begin].path=0;acity[c_begin].allcost=0;que.push_back(c_begin);shortest(que,acity,c_end);deque<int> result;int now=c_end;result.push_front(now);//先将目标城市push,以防起始城==目标城now=acity[now].father_city;while(now!=c_begin){result.push_front(now);now=acity[now].father_city;}result.push_front(c_begin);for(i=0;i<result.size();i++)cout<<result[i]<<" ";cout<<acity[c_end].path<<" "<<acity[c_end].allcost<<endl;return 0;}
- 1030. Travel Plan (30)
- 1030. Travel Plan
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan
- 1030. Travel Plan
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030.Travel Plan
- 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)
- STL源码分析《4》----Traits技术
- UIAcceleration 加速器 UIAcceleration 三维加速数据
- 对Windows程序的事件驱动特性的了解
- Android 异步连网框架
- PL/SQL Developer 远程连接Oracle数据库
- 1030. Travel Plan
- Apache HttpComponents
- Oracle将字符编码从GBK转到UTF8会产生哪些后果,如何操作比较稳妥?
- Python 菜鸟学习第八课
- uva 154 rails
- EAS客户端登录windows server 2008上EAS服务端的端口问题。
- IsPostBack原理
- 我问你,java中的标记接口(Marker interface pattern)有啥用?
- Wow, Mangos登录时的SRP6认证