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
题目大意:
一个旅行者的地图提供了高速公路沿线城市之间的距离,以及每条公路的费用。你的任务是写一个程序来帮助旅游者选择他/她的出发地和目的地之间的最短路径。如果这样的最短路径不是唯一的,你应该输出费用最小的那条最短路径,这保证是唯一的。
输入规格:
每个输入文件包含一个测试用例。每个测试用例第一行包含4个正整数N、M、S和D,其中N(<=500)是城市的数量(因此城市的编号从0到N-1);M是公路的数量;S和D分别是出发城市和终点城市。接下来是M行,每行提供一个高速公路的信息,格式如下:
城市1 城市2 距离 费用
这些数字都是不超过500的整数,并且用一个空格隔开。
输出规格:
对于每个测试用例,打印从起点到终点的最短路径所经过的所有城市,然后是路径的总距离和总费用。这些数字必须用一个空格隔开,并确在结尾没有多余的空格输出。
代码:
DFS:
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
struct node
{
int distance;
int cost;
}tral[501][501];
vector<int> curRoad;
vector<int> minRoad;
int s,e,n,m,visited[501];
int curLength=0;
int minLength=1<<30;
int curCost=0;
int minCost=1<<30;
void DFS(int cur)
{
if(minLength<curLength)
return ;
if(cur==e)
{
int flag=0;
if(curLength<minLength)
{
flag=1;
}
else if(curLength==minLength)
{
if(curCost<minCost)
{
flag=1;
}
}
if(flag==1)
{
minLength=curLength;
minCost=curCost;
minRoad=curRoad;
}
}
for(int i=0;i<n;i++)
{
if(visited[i]==0&&tral[cur][i].distance>0)
{
curLength+=tral[cur][i].distance;
curCost+=tral[cur][i].cost;
visited[i]=1;
curRoad.push_back(i);
DFS(i);
curRoad.pop_back();
visited[i]=0;
curLength-=tral[cur][i].distance;
curCost-=tral[cur][i].cost;
}
}
}
int main()
{
int i,j,k,t,x,y,d,c;
scanf("%d %d %d %d",&n,&m,&s,&e);
for(i=0;i<m;i++)
{
scanf("%d %d %d %d",&x,&y,&d,&c);
tral[x][y].distance=tral[y][x].distance=d;
tral[x][y].cost=tral[y][x].cost=c;
}
visited[s]=1;
curRoad.push_back(s);
DFS(s);
for(i=0;i<minRoad.size();i++)
{
printf("%d ",minRoad[i]);
}
printf("%d ",minLength);
printf("%d",minCost);
return 0;
}
Dijkstra:
#include<stdio.h>
#include<iostream>
#include<stack>
using namespace std;
struct node
{
int l;
int c;
int tag;
}Map[501][501];
int dpl[501],dpc[501],visited[501],path[501];
int n,m,s,e;
void Dijkstra()
{
int i,j;
fill(dpl,dpl+501,0x3f3f3f3f);
fill(dpc,dpc+501,0x3f3f3f3f);
dpl[s]=0;
dpc[s]=0;
for(i=0;i<n;i++)
{
int Min=0x3f3f3f3f;
int index;
for(j=0;j<n;j++)
{
if(!visited[j]&&dpl[j]<Min)
{
Min=dpl[j];
index=j;
}
}
if(Min==0x3f3f3f3f)
break;
visited[index]=1;
for(j=0;j<n;j++)
{
if(!visited[j]&&Map[index][j].tag==1&&(dpl[j]>=Map[index][j].l+dpl[index]))
{
if(dpl[j]>Map[index][j].l+dpl[index])
{
dpl[j]=Map[index][j].l+dpl[index];
dpc[j]=Map[index][j].c+dpc[index];
path[j]=index;
}
else
{
if(dpc[j]>Map[index][j].c+dpc[index])
{
dpc[j]=Map[index][j].c+dpc[index];
path[j]=index;
}
}
}
}
}
}
int main()
{
int i,j,k,t,x,y,l,c;
scanf("%d %d %d %d",&n,&m,&s,&e);
for(i=0;i<m;i++)
{
scanf("%d %d %d %d",&x,&y,&l,&c);
Map[x][y].l=l;
Map[y][x].l=l;
Map[x][y].c=c;
Map[y][x].c=c;
Map[x][y].tag=1;
Map[y][x].tag=1;
}
Dijkstra();
int first=e;
stack<int> stc;
stc.push(first);
while(first!=s)
{
first=path[first];
stc.push(first);
}
while(!stc.empty())
{
printf("%d ",stc.top());
stc.pop();
}
printf("%d %d\n",dpl[e],dpc[e]);
return 0;
}