[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 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
****************************************************************/
#include <iostream>
#include <map>
#include <string>
#define INF 1<<30
using namespace std;
int mat[210][210];
int dist[210];
bool mark[210];
//记录开心值
int happiness[210];
//记录总的开心值
int sumhap[210];
//记录平均开心值
int avg[210];
int rout[210];
//记录有多少条最短路径
int difr[210];
//记录路径长度
int ct[210];
string cities[210];
map<string,int> m;
int num=-1;
int n;
int ston(string s){
	if(m.find(s)!=m.end()) return m[s];
	else{
		num++;
		m[s]=num;
		return num;

	}

}
void init(){
	for(int i=0;i<n;i++){
		dist[i]=INF;
		mark[i]=false;
		happiness[i]=0;
		sumhap[i]=0;
		difr[i]=0;
		avg[i]=0;
		rout[i]=0;
		ct[i]=0;
		for(int j=0;j<n;j++){
			mat[i][j]=INF;
		}
	}
}
void dijk(){

	for(int i=0;i<n;i++){
		int p;
		//选出最短加入路径
		int min=INF;
		for(int j=0;j<n;j++) if(mark[j]==false&&dist[j]<min) min=dist[p=j];
		if(min==INF) return;
		mark[p]=true;
		for(int j=0;j<n;j++){
			if(mark[j]==true) continue;
			if(dist[j]>dist[p]+mat[p][j]){
				dist[j]=dist[p]+mat[p][j];
				rout[j]=p;
				sumhap[j]=sumhap[p]+happiness[j];
				ct[j]=ct[p]+1;
				avg[j]=sumhap[j]/ct[j];
				difr[j]=difr[p];
			}
			else if(dist[j]==dist[p]+mat[p][j]){
				difr[j]+=difr[p];
				if(sumhap[j]<sumhap[p]+happiness[j]){
					rout[j]=p;
					sumhap[j]=sumhap[p]+happiness[j];
					ct[j]=ct[p]+1;
				    avg[j]=sumhap[j]/ct[j];
				}else if(sumhap[j]==sumhap[p]+happiness[j]){
					if(avg[j]<sumhap[j]/(ct[p]+1)){
						rout[j]=p;
						ct[j]=ct[p]+1;
						avg[j]=sumhap[j]/ct[j];

					}

				}
			}
		}
	}

}
void printfrout(int x){
	if(rout[x]!=0) printfrout(rout[x]);

	cout<<cities[rout[x]]<<"->";

}

int main(){
	int k;
	string scity;
	string city;
	int hp;
	string sa,sb;
	int ia,ib,cost;
	while(cin>>n>>k>>scity){
		init();
		num=ston(scity);
		cities[num]=scity;

		for(int i=0;i<n-1;i++){
			cin>>city>>hp;
			num=ston(city);
			happiness[num]=hp;
			cities[num]=city;
		}
		for(int i=0;i<k;i++){
			cin>>sa>>sb>>cost;
			ia=ston(sa);
			ib=ston(sb);
			mat[ia][ib]=mat[ib][ia]=cost;
		}

		dist[0]=0;
		difr[0]=1;
		dijk();
		ib=ston("ROM");
		cout<<difr[ib]<<" "<<dist[ib]<<" "<<sumhap[ib]<<" "<<avg[ib]<<endl;
		printfrout(ib);
		cout<<"ROM"<<endl;
	}
}
/**************************************************************
    author: ws
    Language: C++
****************************************************************/

时间: 2024-08-08 03:39:20

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

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

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 Spec

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

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甲级 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

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(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

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