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 3 0 1 1 20 1 3 2 30 0 3 4 10 0 2 2 20 2 3 1 20Sample Output
0 2 3 3 40
题解:
#include <iostream> #include <vector> #include <algorithm> #include <deque> using namespace std; #define maxlen 10000000 class 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 roads int 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 Cost tmp.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; }