PAT 1087. 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, 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<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<stack>
using namespace std;
stack<int>st;
const int N = 222;
map<string,int>f;
map<int,string>g;
const int inf = 0x3f3f3f3f;
int dp[N],sum[N],hp[N],city[N],pre[N],rout[N],vis[N];
int c[N][N];
int main()
{
	ios::sync_with_stdio(false);
	int n,m,i,j,tmp;
	int u,v;
	string t1,t2,s;
	cin>>n>>m>>s;
	f[s]=1;
	g[1]=s;
	for(i=2;i<=n;i++) {
		cin>>t1>>hp[i];
		f[t1]=i,g[i]=t1;
	}
	pre[1]=-1;
	for(i=1;i<=n;i++) {
		for(j=1;j<=n;j++)
		c[i][j]=c[j][i]=-1;
	}
	while(m--) {
		cin>>t1>>t2>>tmp;
		u=f[t1],v=f[t2];
		c[u][v]=c[v][u]=tmp;
	}
	fill(dp+1,dp+1+n,inf);
	city[1]=1,rout[1]=1,dp[1]=0;
	while(1) {
		u=-1;
		for(i=1;i<=n;i++) {
			if(!vis[i] && (u==-1||dp[i]<dp[u])) u=i;
		}
		if(u==-1) break;
		vis[u]=1;
		for(i=1;i<=n;i++) {
			if(c[u][i]==-1) continue;
			if(dp[u]+c[u][i]<dp[i]) {
				dp[i]=dp[u]+c[u][i];  //最短路劲
				sum[i]=sum[u]+hp[i];  //快乐值 
				rout[i]=rout[u];      //路径数
				city[i]=city[u]+1;    //经过的城市
				pre[i]=u; 
			}
			else if(dp[u]+c[u][i]==dp[i]) {
				rout[i]+=rout[u];
				if(sum[u]+hp[i]>sum[i]) {
					sum[i]=sum[u]+hp[i];
					city[i]=city[u]+1;
					pre[i]=u;
				}
				else if(sum[u]+hp[i]==sum[i] && city[u]+1<city[i]) {
					city[i]=city[u]+1;
					pre[i]=u;
				}
			}
		}
	}
	int p=f["ROM"];
	city[p]--;
	cout<<rout[p]<<" "<<dp[p]<<" "<<sum[p]<<" "<<sum[p]/city[p]<<endl;
	p=pre[p];
	while(p!=-1) {
		st.push(p);
		p=pre[p];
	}
	while(!st.empty()) cout<<g[st.top()]<<"->",st.pop();
	cout<<"ROM"<<endl;
	return 0;
} 






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值