PAT甲級1030 Travel Plan
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;
const int maxn=510;
const int INF=0x3fffffff;
int G[maxn][maxn],cost[maxn][maxn];
int weight[maxn],n,m,st,ed;
int d[maxn],w[maxn],num[maxn],pre[maxn];
bool vis[maxn]={false};
void Dijkstra(int s){
fill(d,d+maxn,INF);
d[s]=0;
w[s]=0;
num[s]=1;
for(int i=0;i<n;i++){
int u=-1,MIN=INF;
for(int j=0;j<n;j++){
if(!vis[j]&&d[j]<MIN){
u=j;
MIN=d[j];
}
}
if(u==-1)break;
vis[u]=true;
for(int v=0;v<n;v++){
if(!vis[v]&&G[u][v]!=INF){
if(d[u]+G[u][v]<d[v]){
d[v]=d[u]+G[u][v];
num[v]=num[u];
w[v]=w[u]+cost[u][v];
pre[v]=u;
} else if(d[u]+G[u][v]==d[v]){
if(w[u]+cost[u][v]<w[v]){
w[v]=w[u]+cost[u][v];
pre[v]=u;
}
num[v]+=num[u]; //無論如何都得加
}
}
}
}
}
void dfs(int v){
if(v==st){
printf("%d",v);
return;
}
dfs(pre[v]);
printf(" %d",v);
}
int main(){
cin>>n>>m>>st>>ed;
fill(G[0],G[0]+maxn*maxn,INF);
while(m--){
int a,b,c,d;
cin>>a>>b>>c>>d;
G[a][b]=c;
G[b][a]=c;
cost[a][b]=d;
cost[b][a]=d;
}
Dijkstra(st);
dfs(ed);
printf(" %d %d\n",d[ed],w[ed]);
return 0;
}
相關文章
- PAT A 1030. Travel Plan (30)【最短路徑】
- 1030 Travel Plan (30分)
- PAT甲級1032 Sharing
- 【Lintcode】1891. Travel Plan
- 浙大PAT甲級考試
- PAT甲級1023 Have Fun with Number
- PAT 甲級 1152 Google Recruitment (20分)GoUI
- 20年春季甲級pat考試
- PAT甲級-1015. Reversible Primes (20)
- PAT甲級1126~1130|C++實現C++
- PAT甲級-1014. Waiting in Line (30)(模擬)AI
- PAT甲級真題1069 數字黑洞(巧妙解法)
- PAT甲級考試題庫題目分類
- 【PAT甲級A1084】Broken Keyboard (20分)(c++)C++
- 2024 秋季PAT認證甲級(題解A1-A4)
- 【PAT甲級A1038】Recover the Smallest Number (30分)(c++)C++
- PAT甲級1122 Hamiltonian Cycle (25分)|C++實現C++
- PAT甲級1154 Vertex Coloring (25分)|C++實現C++
- PAT甲級-1140. Look-and-say Sequence (20)(模擬)
- 2021.9.12週六PAT甲級考試覆盤與總結
- PAT甲級1110 Complete Binary Tree (25分)|C++實現C++
- 19年春季第二題 PAT甲級 1157 Anniversary(25 分)
- PAT-B 1030 完美數列【二分】
- 菜鳥記錄:c語言實現PAT甲級1010--RadixC語言
- 【PAT甲級A1065】A+B and C (64bit) (20分)(c++)C++
- 2020年7月第2題 PAT甲級真題 The Judger (25分)
- (非原創)PAT甲級1123 Is It a Complete AVL Tree (30分)|C++實現C++
- PAT(甲級)2020年秋季考試 7-1 Panda and PP Milk (20分)
- PAT 乙級
- PTA甲級——Be Unique
- 2019年9月8日秋季PAT甲級題解-2-1161-Merging Linked Lists (25 分)
- PAT乙級1023
- 1021 Deepest Root(甲級)
- 【PAT乙級】1027 列印沙漏
- 【PAT乙級】1017 A除以B
- B. Time Travel
- 【PAT乙級】1065 單身狗
- 【PAT乙級】1052 賣個萌