1030 Travel Plan (30分)

最短路径问题。一定要注意将visited[u]是否等于0加入判断!很关键,还是没有理解透,所以才忘了加。(第42行和第45行)

其实图相关的题通过率蛮高的...套路题,坑少。

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<vector>
 4 #define inf 0x3f3f3f3f
 5 using namespace std;
 6 struct Edge{
 7     int next,dis,cos;
 8 };
 9 int main()
10 {
11   int N,M,S,D;
12   scanf("%d%d%d%d",&N,&M,&S,&D);
13   vector<int> V(N);
14   vector<vector<Edge> > E(N);
15   int c1,c2,dis,cos;
16   Edge e;
17   for(int i=0;i<M;i++){
18       scanf("%d%d%d%d",&c1,&c2,&dis,&cos);
19       e.next=c2;
20       e.dis=dis;
21       e.cos=cos;
22       E[c1].push_back(e);
23       e.next=c1;
24       E[c2].push_back(e);
25   }
26   vector<int> visited(N,0);
27   vector<int> pre(N,0);
28   vector<int> distance(N,inf);
29   vector<int> sum(N);
30   distance[S]=0;
31   sum[S]=0;
32   int min,u;
33   for(int i=0;i<N;i++){
34       min=inf;
35       u=S;
36       for(int j=0;j<N;j++){
37           if(visited[j]==0&&min>distance[j]){
38               min=distance[j];
39               u=j;
40           }
41     }
42     visited[u]=1;//!
43     for(int j=0;j<E[u].size();j++){
44         int v=E[u][j].next;
45         if(visited[v]!=0)//!
46           continue;
47         if(distance[u]+E[u][j].dis<distance[v]){
48             distance[v]=distance[u]+E[u][j].dis;
49             pre[v]=u;
50             sum[v]=sum[u]+E[u][j].cos;
51         }
52         else if(distance[u]+E[u][j].dis==distance[v]){
53             if(sum[u]+E[u][j].cos<sum[v]){
54                 sum[v]=sum[u]+E[u][j].cos;
55                 pre[v]=u;
56             }
57         }
58     }
59   }
60   vector<int> res;
61   int k=D;
62   while(k!=S){
63       res.push_back(k);
64       k=pre[k];
65   }
66   res.push_back(S);
67   for(int i=res.size()-1;i>=0;i--)
68     printf("%d ",res[i]);
69   printf("%d ",distance[D]);
70   printf("%d",sum[D]);
71   return 0;
72 }

原文地址:https://www.cnblogs.com/wsshub/p/12565507.html

时间: 2024-10-09 17:16:24

1030 Travel Plan (30分)的相关文章

1030 Travel Plan (30 分)

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 ci

PAT 1030. Travel Plan (30)

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 cit

1030 Travel Plan (30)(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.

1030. Travel Plan (30)

dfs使用vector保存最短路径 时间限制 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 tr

PAT (Advanced Level) 1030. Travel Plan (30)

先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; const int INF=0x7FFFFFFF; const int

PAT:1030. Travel Plan (30) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int MAXV=510; const int INF=0x3fffffff; int N,M,S,D; //城市数,高速公路数,起点,终点 bool visit[MAXV]; //标记在迪杰斯特拉中是否遍历过 int GD[MAXV][MAXV]; //存储距离图 int GC[MAXV][MAXV];

A1030 Travel Plan (30分)

一.技术总结 这一题是关于图的遍历,但是涉及Djikstra算法,在求最短路径的同时,还要把路径记录下来:同时增加了边权,也就会每个城市之间的花费: 这里采用Djikstra算法+DFS遍历的方法 第一步使用Djikstra算法求出最短路径,使用vector类型pre数组进行存储,每个结点里面保存的是他们的前驱结点. 然后再使用DFS遍历pre,然后会有得出遍历树. 要注意下标是0~n-1,还是1~n. 细节问题参考代码 二.参考代码 #include<bits/stdc++.h> using

pat1030. Travel Plan (30)

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

1030 Travel Plan (30 分)

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 ci