程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> [PAT]1087. All Roads Lead to Rome (30)

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

編輯:C++入門知識

[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 
#include 
#include 
#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 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;idist[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]";

}

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>city>>hp;
			num=ston(city);
			happiness[num]=hp;
			cities[num]=city;
		}
		for(int i=0;i>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<						

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved