1087. All Roads Lead to Rome (30)

时间限制

200 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (2<=N<=200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N-1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format "City1 City2 Cost". Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.

Output Specification:

For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommended. If such a route is still not unique, then we output the one with the maximum average happiness -- it is guaranteed by the judge that such a solution exists and is unique.

Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommended route. Then in the next line, you are supposed to print the route in the format "City1->City2->...->ROM".

Sample Input:

6 7 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH PRS 1

Sample Output:

3 3 195 97
HZH->PRS->ROM
  1 #include<stdio.h>
  2 #include<map>
  3 #include<string>
  4 #include<string.h>
  5 #include<stack>
  6 using namespace std;
  7 #define MAX 210
  8 int INF = 1000000;
  9 int HappyVal[210];
 10 int visit[MAX];
 11 int Grap[MAX][MAX];
 12 int d[MAX];
 13 int h[MAX];
 14 int num[MAX];
 15 int pre[MAX];
 16 int Count[MAX];
 17
 18 void Dijkstra(int Begin,int NodeNum)
 19 {
 20     d[Begin] = 0;
 21     h[Begin] = HappyVal[Begin];
 22     num[Begin] = 1;
 23     Count[Begin] = 0;
 24     for(int i = 0;i < NodeNum ;i++)
 25     {
 26         int index = -1;
 27         int MIN = INF;
 28         for(int j = 0 ;j <NodeNum ;j++)
 29         {
 30             if(!visit[j] && d[j] < MIN)
 31             {
 32                 index = j;
 33                 MIN = d[j];
 34             }
 35         }
 36
 37         if(index == -1) return ;
 38         visit[index] = true;
 39         for(int v = 0 ;v <NodeNum ;v++)
 40         {
 41             if(!visit[v] && Grap[index][v]!=INF)
 42             {
 43                 if(d[index]+Grap[index][v]<d[v])
 44                 {
 45                     d[v] = d[index]+Grap[index][v];
 46                     num[v] = num[index];
 47                     h[v] = h[index] + HappyVal[v];
 48                     pre[v] = index;
 49                     Count[v] = Count[index] +1;
 50                 }
 51                 else if(d[index]+Grap[index][v]==d[v])
 52                 {
 53                     num[v] = num[v] + num[index];
 54
 55                     if(h[v] < h[index] + HappyVal[v])
 56                     {
 57                         h[v] = h[index] + HappyVal[v];
 58                         Count[v] = Count[index] +1;
 59                         pre[v] = index;
 60                     }
 61                     else if( h[v] == h[index] + HappyVal[v] && (double)(h[index] + HappyVal[v])/(Count[index]+1) > (double)h[v]/Count[v])
 62                     {
 63                         Count[v] = Count[index] +1;
 64                         pre[v] = index;
 65                     }
 66                 }
 67             }
 68         }
 69     }
 70
 71 }
 72
 73 int main()
 74 {
 75     int i,j,N,K,happy,ROM;
 76     char Begin[4],tem[4];
 77     scanf("%d%d%s",&N,&K,Begin);
 78     map<string,int> mm;
 79     map<int,string> mm2;
 80     mm[Begin] = 0;
 81     mm2[0] = Begin ;
 82     HappyVal[mm[Begin]] = 0;
 83     for(i = 1 ; i < N ;i++)
 84     {
 85         scanf("%s%d",tem,&happy);
 86         if(strcmp("ROM",tem)==0) ROM = i;
 87         mm[tem] = i;
 88         mm2[i] = tem;
 89         HappyVal[i] = happy;
 90     }
 91
 92     char x[4],y[4];
 93
 94     for(i = 0 ; i < N ;i++)
 95     {
 96         for(j = 0 ; j < N ;j++)
 97         {
 98             Grap[i][j] = INF;
 99         }
100         d[i] = h[i] = INF;
101         pre[i] = -1;
102         Count[i] = 0;
103     }
104
105     for(i = 0 ; i < K ;i++)
106     {
107         scanf("%s%s",x,y);
108         scanf("%d",&Grap[mm[x]][mm[y]]);
109         Grap[mm[y]][mm[x]] = Grap[mm[x]][mm[y]];
110     }
111
112     Dijkstra( mm[Begin] , N);
113
114     printf("%d %d %d %d\n",num[mm["ROM"]],d[mm["ROM"]],h[mm["ROM"]],h[mm["ROM"]]/Count[mm["ROM"]]);
115
116     stack<int> ss;
117     i= mm["ROM"];
118     while(i != -1)
119     {
120         ss.push(i);
121         i = pre[i];
122     }
123     int fir = 1;
124     while(!ss.empty())
125     {
126         if(fir == 1)
127         {
128             fir = 0;
129             printf("%s",mm2[ss.top()].c_str());
130         }
131         else printf("->%s",mm2[ss.top()].c_str());
132         ss.pop();
133     }
134
135     printf("\n");
136
137     return 0;
138 }
时间: 2024-08-08 04:58:35

1087. All Roads Lead to Rome (30)的相关文章

[PAT]1087. All Roads Lead to Rome (30)

/************************************************************** 1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different tourist routes from our city to Rome. You are suppos

1087. All Roads Lead to Rome (30)【最短路】——PAT (Advanced Level) Practise

题目信息 1087. All Roads Lead to Rome (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness. I

PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace st

pat1087. All Roads Lead to Rome (30)

1087. All Roads Lead to Rome (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gain

PAT 1087 All Roads Lead to Rome

1 #include <cstdio> 2 #include <climits> 3 #include <iostream> 4 #include <vector> 5 #include <string> 6 #include <queue> 7 #include <unordered_map> 8 #include <algorithm> 9 10 using namespace std; 11 12 typ

PAT (Advanced Level) 1087 All Roads Lead to Rome

题解   最短路径经典题型.套最短路的板子再加上额外的要求就可以了(说起来好简单).SPFA也行,Dijkstra也可以.这里我用的是SPFA.因为题目要求,将地名和其对应的数字用map映射一下,这样方便处理. same[i]代表到达地点 i 有几种路径: dist[i]代表从起点到地点 i 的最短距离: happy[i]代表从起点到地点 i 的幸福值: cnt[i]代表从起点到地点 i 需要经过几个城市: ans[i]代表从哪个地点到达了地点 i : 代码 #include<bits/stdc

PAT甲级 All Roads Lead to Rome (dijkstra+dfs回溯)

All Roads Lead to Rome 本题需要记录一共有几条最短路径,并输出最短路中开心值最大的路径或者开心值相等的情况下输出平均开心值最大的路径. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <vector> 6 #include <map> 7 #include <algori

PAT1087. All Roads Lead to Rome

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness. Input Specification: Each input file contains one test case. For each case, th

Pat(Advanced Level)Practice--1087(All Roads Lead to Rome)

Pat1087代码 题目描述: Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness. Input Specification: Each input file contains one test case. F