1030. Travel Plan (30)
2016-03-13 22:29
274 查看
1030. 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
求最短路径问题,且最短路径在不止一条的时候,选出cost最小的一条路径。使用dijkstra或者dfs都可以。由于dfs更简单一点,所以就用dfs来求解了。
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
void dfs(int st,int dis,int cos);
int road[501][501];
int cost[501][501];
int visited[501]={};
int num_city,num_road,st,en;
int dis_min=0,cost_min=0;
vector<int> tmp;
vector<int> record;
int main()
{
cin >>num_city >>num_road >>st >>en;
memset(&road[0][0],-1,sizeof(road));
memset(&cost[0][0],0,sizeof(cost));
for(int i=0;i<num_road;i++)
{
int a,b,dis,c;
cin >>a >>b >>dis >>c;
road[a][b]=dis;
road[b][a]=dis;
cost[a][b]=c;
cost[b][a]=c;
}
tmp.push_back(st);
visited[st]=1;
dfs(st,0,0);
vector<int>::iterator iter=record.begin();
cout <<*iter;
iter++;
for(;iter!=record.end();iter++)
cout <<" "<<*iter;
cout <<" "<<dis_min <<" " <<cost_min <<endl;
return 0;
}
void dfs(int st,int dis,int cos)
{
if(st==en)
{
if(dis<dis_min || dis_min==0)
{
dis_min=dis;
cost_min=cos;
record=tmp;
}
if(dis==dis_min)
{
if(cos<cost_min)
{
cost_min=cos;
record=tmp;
}
}
}
for(int i=0;i<num_city;i++)
{
if(road[st][i]!=-1 && visited[i]!=1)
{
visited[i]=1;
tmp.push_back(i);
dfs(i,dis+road[st][i],cos+cost[st][i]);
visited[i]=0;
tmp.pop_back();
}
}
}
相关文章推荐
- libdvbpsi源码分析(四)PAT表解析/重建
- PAT配置
- 什么是端口复用动态地址转换(PAT) 介绍配置实例
- MikroTik layer7-protocol
- PAT是如何工作的
- PAT 乙级题:1002. 写出这个数 (20)
- PAT (Advanced Level) Practise 1001-1010
- 数据结构学习与实验指导(一)
- PAT Basic Level 1001-1010解题报告
- 1001. 害死人不偿命的(3n+1)猜想
- 1002. 写出这个数
- 1032. 挖掘机技术哪家强
- 1001. 害死人不偿命的(3n+1)猜想 (PAT basic)
- 1002. 写出这个数(PAT Basic)
- 1004. 成绩排名(PAT Basic)
- 1006. 换个格式输出整数(PAT Basic)
- 1007. 素数对猜想(PAT Basic)
- 1008. 数组元素循环右移问题
- 1009. 说反话(PAT Basic)
- 1011. A+B和C(PAT Basic)