1030. Travel Plan (30)
2015-08-26 21:25
218 查看
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
Sample Output
提交代码
————————————
在这里有前面一点,就是最小生成树的问题。
这里仅仅是将路径保存下来而已。
第一次提交的时候,是下面的代码。
结果是,
显然是因为自己没有更显cost的数值。
在孙大神那里学到了一些知识,是关于vector的应用函数的,自己平时没有怎么用过这些,所以这里就学习一下他。
主要是vector的操作函数。;。。。。
______clear();
______resize(N);
______assign(N,false);
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> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ #define MAX 1010 #define INF 0x7fffffff int N,M,S,D; int dist[MAX][MAX],cost[MAX][MAX]; int mydist[MAX],mycost[MAX]; int path[MAX]; void myinit(){ for(int i=0; i<N; i++){ for(int j=0; j<N; j++){ dist[i][j]=cost[i][j]=MAX; } mydist[i]=mycost[i]=MAX; path[i]=0; } for(int i=0; i<M; i++){ int c1,c2,d,c; cin>>c1>>c2>>d>>c; dist[c1][c2]=dist[c2][c1]=d; cost[c1][c2]=cost[c2][c1]=c; } } void dij(int s){ mydist[s]=0; mycost[s]=0; for(int t=0; t<N; t++){ for(int i=0; i<N; i++){ for(int j=0; j<N; j++){ if(mydist[i]!=MAX){ if(mydist[i] + dist[i][j]<mydist[j]){ mydist[j]=mydist[i]+dist[i][j]; path[i]=j; }else if(mydist[i]+dist[i][j]==mydist[j] && mycost[i]!=MAX && mycost[i]+cost[i][j]<mycost[j]){ mycost[j]=mycost[i]+cost[i][j]; path[i]=j; } } } } } cout<<s<<" "; int end=s; while(end != D){ cout<<path[end]<<" "; end=path[end]; } cout<<mydist[D]<<" "<<mycost[D]<<endl; } int main(int argc, char** argv) { cin>>N>>M>>S>>D; myinit(); dij(S); return 0; }
结果是,
显然是因为自己没有更显cost的数值。
在孙大神那里学到了一些知识,是关于vector的应用函数的,自己平时没有怎么用过这些,所以这里就学习一下他。
#include <iostream> #include <vector> #include <stack> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ #define MAX 1010 #define INF 0x7fffffff struct node{ int dis,cost,path; node(){ dis=INF;cost=INF;path=-1; } }; struct edge{ int end,dis,cost; edge(int e, int d, int c){ end=e;dis=d;cost=c; } }; int N,M,S,D; int dist[MAX][MAX],cost[MAX][MAX]; int mydist[MAX],mycost[MAX]; vector<vector<edge> > edg; vector<int> path; vector<node> city; vector<bool> visited; void myinit(){ edg.clear(); edg.resize(N); for(int i=0; i<M; i++){ int c1,c2,d,c; cin>>c1>>c2>>d>>c; edg[c1].push_back(edge(c2,d,c)); edg[c2].push_back(edge(c1,d,c)); dist[c1][c2]=dist[c2][c1]=d; cost[c1][c2]=cost[c2][c1]=c; } } int findmin(){ int nmin=INF; int k=-1; for(int i=0; i<N; i++){ if(!visited[i] && city[i].dis<nmin){ nmin=city[i].dis; k=i; } } return k; } void dij(int s){ visited.assign(N,false); city.clear(); city.resize(N); city[s].dis=0;city[s].cost=0; while(1){ int u=findmin(); if(u==-1) break; visited[u]=true; for(int i=0; i<edg[u].size(); i++){ int v=edg[u][i].end; int cost=edg[u][i].cost; int dis=edg[u][i].dis; if(!visited[v]){ if(city[v].dis>city[u].dis+dis){ city[v].dis=city[u].dis+dis; city[v].cost=city[u].cost+cost; city[v].path=u; }else if(city[v].dis==city[u].dis+dis && city[v].cost>city[u].cost+cost){ city[v].cost=city[u].cost+cost; city[v].path=u; } } } } stack<int> ans; ans.push(D); int dd=D; while(city[dd].path!=-1){ dd=city[dd].path; ans.push(dd); } while(!ans.empty()){ cout<<ans.top()<<" "; ans.pop(); } cout<<city[D].dis<<" "<<city[D].cost<<endl; } int main(int argc, char** argv) { cin>>N>>M>>S>>D; myinit(); dij(S); return 0; }
主要是vector的操作函数。;。。。。
______clear();
______resize(N);
______assign(N,false);
相关文章推荐
- SAX解析XML文件步骤及问题
- POJ 1651 Multiplication Puzzle (区间DP)
- IOS零碎知识点
- poj 1321 棋盘问题 dfs
- Magic Number(Levenshtein distance算法)
- 三道题 2015.8.26(二进制中1的个数,数组中出现次数超过一半的数字,最大(小)的K个数)
- poj 3685 Matrix 二分里套二分
- 21 Merge Two Sorted Lists
- cf Div#317 Lengthening Sticks
- C++中虚继承派生类构造函数的正确写法
- xcode快捷键
- 跨域资源共享(CORS)安全性浅析[内有提及OPTIONS请求发起的条件]
- Reduce侧联接
- 折叠Collapse插件
- linux 根据sp和fp查调用栈
- html 利用 frameset 进行简单的框架布局
- PHP的curl带验证码模拟登陆学校的教务系统(验证码已被自动识别,不需要输入)
- 02类型转化 进程空间 数组2015/08/26
- 设计模式之策略模式
- 最新制作 Mac 电脑 U 盘启动盘笔记