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 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<bits/stdc++.h>
using namespace std;
#define MaxNum 505
#define INF 1000000000
struct Node{
int d,pr;
Node(){
d=INF;
pr=INF;
}
}G[MaxNum][MaxNum];
int n,m,s,d,dist[MaxNum],price[MaxNum]={0},parent[MaxNum];
bool vis[MaxNum]={false};
void Dijkstra(int S,int E){
fill(parent,parent+MaxNum,-1);
fill(dist,dist+MaxNum,INF);
dist[S]=0;
for(int i=0;i<n;i++){
int u=-1,Min=INF;
for(int j=0;j<n;j++){
if(vis[j]==false&&dist[j]<Min){
u=j;
Min=dist[j];
}
}
if(u==-1)return;
if(u!=S)price[u]=price[parent[u]]+G[parent[u]][u].pr;
vis[u]=true;
for(int j=0;j<n;j++){
if(vis[j]==false&&G[u][j].d!=INF){
if(G[u][j].d+dist[u]<dist[j]){
dist[j]=G[u][j].d+dist[u];
parent[j]=u;
}else if(G[u][j].d+dist[u]==dist[j]){
if(price[parent[j]]+G[parent[j]][j].pr>price[u]+G[u][j].pr){
parent[j]=u;
}
}
}
}
}
}
int main(){
scanf("%d%d%d%d",&n,&m,&s,&d);
int v,u,w,p;
for(int i=0;i<m;i++){
scanf("%d%d%d%d",&v,&u,&w,&p);
G[u][v].d=G[v][u].d=w;
G[u][v].pr=G[v][u].pr=p;
}
Dijkstra(s,d);
int x=d;
stack<int>st;
while(x!=-1){
st.push(x);
x=parent[x];
}
while(!st.empty()){
printf("%d ",st.top());
st.pop();
}
printf("%d %d",dist[d],price[d]);
return 0;
}
以前写的
#include<bits/stdc++.h>
using namespace std;
int n,v,s,e;
int vertex[505][505],dist[505],price[505][505],pc[505]={0},path[505];
bool visit[505]= {false};
int FindMin(int n){
int Min=65535,MinV;
for(int i=0;i<n;i++){
if(visit[i]==false&&dist[i]<Min){
Min=dist[i];
MinV=i;
}
}
if(Min==65535)
return -1;
else
return MinV;
}
int main() {
cin>>n>>v>>s>>e;
fill(vertex[0],vertex[0]+505*505,65535);
fill(dist,dist+505,65535);
fill(path,path+505,-1);
int a,b,c,d;
for(int i=0; i<v; i++) {
cin>>a>>b>>c>>d;
vertex[a][b]=c;
vertex[b][a]=c;
price[a][b]=d;
price[b][a]=d;
}
for(int i=0;i<n;i++){
if(vertex[s][i]!=65535){
dist[i]=vertex[s][i];
path[i]=s;
pc[i]+=price[s][i];
}
}
visit[s]=true;
dist[s]=0;
while(1){
int V=FindMin(n);
if(V==-1)break;
visit[V]=true;
for(int i=0;i<n;i++){
if(visit[i]==false&&dist[i]>vertex[V][i]+dist[V]){
dist[i]=vertex[V][i]+dist[V];
pc[i]=price[V][i]+pc[V];
path[i]=V;
}else if(visit[i]==false&&dist[i]==(vertex[V][i]+dist[V])){
if(pc[i]>pc[V]+price[V][i]){
pc[i]=price[V][i]+pc[V];
path[i]=V;
}
}
}
}
stack<int>st;
int count=0,q=e;
while(path[e]!=-1){
st.push(e);
count++;
e=path[e];
}
st.push(s);
for(int i=0;i<count+1;i++){
if(i!=0)printf(" ");
printf("%d",st.top());
st.pop();
}
printf(" %d %d",dist[q],pc[q]);
}