PAT - 甲级 - 1087. All Roads Lead to Rome (30)(Dijkstra+DFS+路径)
2017-11-03 11:18
399 查看
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:
Sample Output:
题目要求:
给定节点和节点间连线,连线的权,节点的权。
1.求最短路,路径+条数。
2.最短路不唯一时,求出节点权值和最大情况。
3.最短路中存在最大节点权值和相同的情况,求出平均权值最大的情况。
求解:
1.Dijkstra求最短路,记录所有路径,并记录最短路条数
2.DFS遍历所有最短路求出符合2.3的情况,并记录
3.输出结果
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#define INF 999999999
using namespace std;
int n, k, e[210][210], dis[210], routs[210];
int happiness[210];
bool vis[210];
map<string, int> strToint;
map<int, string> intTostr;
vector<int> pre[210];
vector<int> tpath, path;
int maxs = -1;
double maxa = -1;
void dfs(int v) {
tpath.push_back(v);
if(v == 0) {
int tmaxs = 0;
for(int i = 0; i < tpath.size(); i++) {
tmaxs += happiness[tpath[i]];
}
double tmaxa = (1.0*tmaxs) / (1.0*(tpath.size()-1));
if(tmaxs > maxs) {
maxs = tmaxs;
maxa = tmaxa;
path = tpath;
} else if(tmaxs == maxs) {
if(maxa < tmaxa) {
maxa = tmaxa;
path = tpath;
}
}
tpath.pop_back();
return ;
}
for(int i = 0; i < pre[v].size(); i++) {
dfs(pre[v][i]);
}
tpath.pop_back();
}
int main() {
freopen("input.txt", "r", stdin);
fill(e[0], e[0]+210*210, INF);
fill(dis, dis+210, INF);
fill(routs, routs+210, 0);
fill(vis, vis+210, false);
string temp, from, to;
int tdis;
scanf("%d%d", &n, &k);
cin>>temp;
strToint[temp] = 0;
intTostr[0] = temp;
for(int i = 1; i < n; i++) {
cin>>temp;
strToint[temp] = i;
intTostr[i] = temp;
scanf("%d", &happiness[i]);
}
for(int i = 0; i < k; i++) {
cin>>from>>to>>tdis;
int idf = strToint[from];
int idt = strToint[to];
e[idf][idt] = e[idt][idf] = tdis;
}
dis[0] = 0; routs[0] = 1;
for(int i = 0; i < n; i++) {
int x = -1, mindis = INF;
for(int j = 0; j < n; j++) {
if(vis[j] == false && dis[j] < mindis) {
mindis = dis[j];
x = j;
}
}
if(x == -1) break;
vis[x] = true;
for(int y = 0; y < n; y++) {
if(vis[y] == false && e[x][y] != INF) {
if(dis[y] > dis[x] + e[x][y]) {
routs[y] = routs[x];
dis[y] = dis[x] + e[x][y];
pre[y].clear();
pre[y].push_back(x);
} else if (dis[y] == dis[x] + e[x][y]) {
routs[y] += routs[x];
pre[y].push_back(x);
}
}
}
}
int ROM = strToint["ROM"];
dfs(ROM);
printf("%d %d %d %d\n", routs[ROM],dis[ROM], maxs, (int)maxa);
for(int i = path.size()-1; i > 0; i--) {
int id = path[i];
cout<<intTostr[id]<<"->";
}
cout<<"ROM"<<endl;
return 0;
}
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.求最短路,路径+条数。
2.最短路不唯一时,求出节点权值和最大情况。
3.最短路中存在最大节点权值和相同的情况,求出平均权值最大的情况。
求解:
1.Dijkstra求最短路,记录所有路径,并记录最短路条数
2.DFS遍历所有最短路求出符合2.3的情况,并记录
3.输出结果
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#define INF 999999999
using namespace std;
int n, k, e[210][210], dis[210], routs[210];
int happiness[210];
bool vis[210];
map<string, int> strToint;
map<int, string> intTostr;
vector<int> pre[210];
vector<int> tpath, path;
int maxs = -1;
double maxa = -1;
void dfs(int v) {
tpath.push_back(v);
if(v == 0) {
int tmaxs = 0;
for(int i = 0; i < tpath.size(); i++) {
tmaxs += happiness[tpath[i]];
}
double tmaxa = (1.0*tmaxs) / (1.0*(tpath.size()-1));
if(tmaxs > maxs) {
maxs = tmaxs;
maxa = tmaxa;
path = tpath;
} else if(tmaxs == maxs) {
if(maxa < tmaxa) {
maxa = tmaxa;
path = tpath;
}
}
tpath.pop_back();
return ;
}
for(int i = 0; i < pre[v].size(); i++) {
dfs(pre[v][i]);
}
tpath.pop_back();
}
int main() {
freopen("input.txt", "r", stdin);
fill(e[0], e[0]+210*210, INF);
fill(dis, dis+210, INF);
fill(routs, routs+210, 0);
fill(vis, vis+210, false);
string temp, from, to;
int tdis;
scanf("%d%d", &n, &k);
cin>>temp;
strToint[temp] = 0;
intTostr[0] = temp;
for(int i = 1; i < n; i++) {
cin>>temp;
strToint[temp] = i;
intTostr[i] = temp;
scanf("%d", &happiness[i]);
}
for(int i = 0; i < k; i++) {
cin>>from>>to>>tdis;
int idf = strToint[from];
int idt = strToint[to];
e[idf][idt] = e[idt][idf] = tdis;
}
dis[0] = 0; routs[0] = 1;
for(int i = 0; i < n; i++) {
int x = -1, mindis = INF;
for(int j = 0; j < n; j++) {
if(vis[j] == false && dis[j] < mindis) {
mindis = dis[j];
x = j;
}
}
if(x == -1) break;
vis[x] = true;
for(int y = 0; y < n; y++) {
if(vis[y] == false && e[x][y] != INF) {
if(dis[y] > dis[x] + e[x][y]) {
routs[y] = routs[x];
dis[y] = dis[x] + e[x][y];
pre[y].clear();
pre[y].push_back(x);
} else if (dis[y] == dis[x] + e[x][y]) {
routs[y] += routs[x];
pre[y].push_back(x);
}
}
}
}
int ROM = strToint["ROM"];
dfs(ROM);
printf("%d %d %d %d\n", routs[ROM],dis[ROM], maxs, (int)maxa);
for(int i = path.size()-1; i > 0; i--) {
int id = path[i];
cout<<intTostr[id]<<"->";
}
cout<<"ROM"<<endl;
return 0;
}
相关文章推荐
- 1030. Travel Plan (30)-PAT甲级真题(Dijkstra + DFS,输出路径,边权)
- 1018. Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS)
- PAT - 甲级 - 1030. Travel Plan (30) (Dijkstra+DFS)
- 1111. Online Map (30)-PAT甲级真题(Dijkstra + DFS)
- PAT甲题题解-1111. Online Map (30)-PAT甲级真题(模板题,两次Dijkstra,同时记下最短路径)
- PAT 1018. Public Bike Management (30)(Dijkstra,dfs根据pre[]输出路径,双向计算)
- 1087. All Roads Lead to Rome (30)-PAT甲级真题-Dijkstra + DFS
- PAT - 甲级 - 1018. Public Bike Management (30)(Dijkstra+DFS)
- PAT (Advanced Level) 1072. Gas Station (30) Dijkstra最短路径+剪枝
- PAT (Advanced Level) 1087. All Roads Lead to Rome (30) 条条大路通罗马 Dijkstra+DFS
- PAT 1030. Travel Plan (30)(Dijkstra,最短路径的同时计算最小奥cost)
- PAT (Advanced Level) 1053. Path of Equal Weight (30) 求树根到叶子和为所给数值的路径,DFS后排序
- PAT - 甲级 1111. Online Map (30)(Dijkstra+DFS)
- PAT 甲级 1004. Counting Leaves (30) DFS
- PAT (Advanced Level) 1030. Travel Plan (30) Dijkstra最短路径
- 1018. Public Bike Management (30)dijkstra记录路径+dfs最优值查找
- PAT 甲级 1021. Deepest Root(dfs:无向图的最远路径、连通分量个数)
- 1115. Counting Nodes in a BST (30)-PAT甲级真题(二叉树的遍历,dfs)
- PAT - 甲级 - 1053. Path of Equal Weight (30)(树的DFS搜索+排序)
- 1004. Counting Leaves (30)-PAT甲级真题(bfs,dfs,树的遍历,层序遍历)