码迷,mamicode.com
首页 > 其他好文 > 详细

PAT_A1111#Online Map

时间:2019-06-16 18:49:19      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:ice   com   bin   lan   temp   ecif   printf   size   rap   

Source:

PAT A1111 Online Map (30 分)

Description:

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

V1 V2 one-way length time

where V1 and V2 are the indices (from 0 to N1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:

For each case, first print the shortest path from the source to the destination with distance D in the format:

Distance = D: source -> v1 -> ... -> destination

Then in the next line print the fastest path with total time T:

Time = T: source -> w1 -> ... -> destination

In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

Distance = D; Time = T: source -> u1 -> ... -> destination

Sample Input 1:

10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5

Sample Output 1:

Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5

Sample Input 2:

7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5

Sample Output 2:

Distance = 3; Time = 4: 3 -> 2 -> 5

Keys:

  • 最短路径

Attention:

  • 其实就是求两次最短路径,代码code一遍再copy一遍,考试的时候怎么快怎么来,优化代码反而容易出错

Code:

  1 /*
  2 Data: 2019-06-16 17:40:10
  3 Problem: PAT_A1111#Online Map
  4 AC: 42:06
  5 
  6 题目大意:
  7 给出当前位置和目的地,给出最短路径和最快路径
  8 输入:
  9 第一行给出,结点数N,边数M
 10 接下来M行,v1,v2,单/双向,距离,时间
 11 最后一行,起点,终点
 12 输出:
 13 最短距离,输出路径,不唯一则选择最快的
 14 最短时间,输出路径,不唯一则选择经过结点最少的
 15 */
 16 
 17 #include<cstdio>
 18 #include<vector>
 19 #include<algorithm>
 20 using namespace std;
 21 const int M=1e3,INF=1e9;
 22 int grap[M][M],cost[M][M],vis[M],d[M];
 23 int n,v1,v2,optValue=INF,optCost=INF;
 24 vector<int> pre[M],temp,opt,pro[M],fav;
 25 
 26 void DijskraP(int s)
 27 {
 28     fill(vis,vis+M,0);
 29     fill(d,d+M,INF);
 30     d[s]=0;
 31     for(int i=0; i<n; i++)
 32     {
 33         int u=-1,Min=INF;
 34         for(int j=0; j<n; j++)
 35         {
 36             if(vis[j]==0 && d[j]<Min)
 37             {
 38                 Min=d[j];
 39                 u=j;
 40             }
 41         }
 42         if(u==-1)
 43             return;
 44         vis[u]=1;
 45         for(int v=0; v<n; v++)
 46         {
 47             if(vis[v]==0 && grap[u][v]!=INF)
 48             {
 49                 if(d[u]+grap[u][v] < d[v])
 50                 {
 51                     d[v] = d[u]+grap[u][v];
 52                     pre[v].clear();
 53                     pre[v].push_back(u);
 54                 }
 55                 else if(d[u]+grap[u][v] == d[v])
 56                     pre[v].push_back(u);
 57             }
 58         }
 59     }
 60 }
 61 
 62 void DFSP(int v)
 63 {
 64     if(v == v1)
 65     {
 66         temp.push_back(v);
 67         int value=0;
 68         for(int i=temp.size()-1; i>0; i--)
 69         {
 70             int id=temp[i],idNext=temp[i-1];
 71             value += cost[id][idNext];
 72         }
 73         if(value < optValue)
 74         {
 75             optValue = value;
 76             opt = temp;
 77         }
 78         temp.pop_back();
 79         return;
 80     }
 81     temp.push_back(v);
 82     for(int i=0; i<pre[v].size(); i++)
 83         DFSP(pre[v][i]);
 84     temp.pop_back();
 85 }
 86 
 87 void DijskraC(int s)
 88 {
 89     fill(vis,vis+M,0);
 90     fill(d,d+M,INF);
 91     d[s]=0;
 92     for(int i=0; i<n; i++)
 93     {
 94         int u=-1,Min=INF;
 95         for(int j=0; j<n; j++)
 96         {
 97             if(vis[j]==0 && d[j]<Min)
 98             {
 99                 Min=d[j];
100                 u=j;
101             }
102         }
103         if(u==-1)
104             return;
105         vis[u]=1;
106         for(int v=0; v<n; v++)
107         {
108             if(vis[v]==0 && cost[u][v]!=INF)
109             {
110                 if(d[u]+cost[u][v] < d[v])
111                 {
112                     d[v] = d[u]+cost[u][v];
113                     pro[v].clear();
114                     pro[v].push_back(u);
115                 }
116                 else if(d[u]+cost[u][v] == d[v])
117                     pro[v].push_back(u);
118             }
119         }
120     }
121 }
122 
123 void DFSC(int v)
124 {
125     if(v == v1)
126     {
127         temp.push_back(v);
128         if(temp.size() < optCost)
129         {
130             optCost = temp.size();
131             fav = temp;
132         }
133         temp.pop_back();
134         return;
135     }
136     temp.push_back(v);
137     for(int i=0; i<pro[v].size(); i++)
138         DFSC(pro[v][i]);
139     temp.pop_back();
140 }
141 
142 int main()
143 {
144 #ifdef    ONLINE_JUDGE
145 #else
146     freopen("Test.txt", "r", stdin);
147 #endif
148 
149     fill(grap[0],grap[0]+M*M,INF);
150     fill(cost[0],cost[0]+M*M,INF);
151 
152     int m,one;
153     scanf("%d%d", &n,&m);
154     for(int i=0; i<m; i++)
155     {
156         scanf("%d%d%d", &v1,&v2,&one);
157         scanf("%d%d", &grap[v1][v2],&cost[v1][v2]);
158         if(!one)
159         {
160             grap[v2][v1]=grap[v1][v2];
161             cost[v2][v1]=cost[v1][v2];
162         }
163     }
164     scanf("%d%d",&v1,&v2);
165     DijskraP(v1);
166     DFSP(v2);
167     printf("Distance = %d", d[v2]);
168     DijskraC(v1);
169     DFSC(v2);
170     if(opt == fav)
171     {
172         printf("; Time = %d: %d",d[v2],opt[opt.size()-1]);
173         for(int i=opt.size()-2; i>=0; i--)
174             printf(" -> %d", opt[i]);
175     }
176     else
177     {
178         printf(": %d", opt[opt.size()-1]);
179         for(int i=opt.size()-2; i>=0; i--)
180             printf(" -> %d", opt[i]);
181         printf("\n");
182         printf("Time = %d: %d", d[v2],fav[fav.size()-1]);
183         for(int i=fav.size()-2; i>=0; i--)
184             printf(" -> %d", fav[i]);
185     }
186 
187 
188     return 0;
189 }

 

PAT_A1111#Online Map

标签:ice   com   bin   lan   temp   ecif   printf   size   rap   

原文地址:https://www.cnblogs.com/blue-lin/p/11032445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!