PAT甲级1002 All Roads Lead to Rome

All Roads Lead to Rome (30)
时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)
题目描述
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.

输入描述:
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.

输出描述:
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”.

输入例子:
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

输出例子:
3 3 195 97
HZH->PRS->ROM
思路:一开始准备用第一题的思路做,后来有5个样例不过,看了讨论才发现,统计最短路径条数不能只讨论倒数第二个点,因为中间可能存在一些点有多条路径。所以求最短路径条数应该在松弛过程中维护。
注:city[i]表示i号城市的happy值,
happy[i]表示i号节点的最短路径的happy总值,
cnt[i]表示i号节点最短路径中city数,
scheme[i]表示i号节点最短路径条数
name[s]表示城市名称s的编号
index[i]表示编号为i的城市名称
还有一点:就是happy平均值不能用int存,注意看题,题目要求只是最后输出整数部分,比较时仍需要用double

#include 
#include 
#include
#include 
using namespace std;
const int N = 205, INF = 1000000000;
int n, m, dest;
int g[N][N], dist[N],path[N], happy[N],cnt[N],scheme[N],city[N];  
bool st[N];     // 存储每个点的最短距离是否已确定
string index[N];
unordered_map<string,int>name;

void dijkstra()
{
    for (int i = 0; i<=n; i++) dist[i] = INF;
    dist[0] = 0; //cnt[0]=0;
    scheme[0]=1;
    for (int i = 0; i<=n; i++)
    {
        int id, mind = INF;
        for (int j = 0; j <= n; j++)
            if (!st[j] && dist[j] < mind)
            {
                mind = dist[j];
                id = j;
            }
        st[id] = 1;
        for (int j = 0; j <= n; j++)
        {
        	if(dist[id]+g[id][j]<dist[j])
        	{
        		dist[j]=dist[id]+g[id][j]; //更新最短路径长度 
        		path[j]=id;                //更新路径 
				happy[j]=happy[id]+city[j];
				cnt[j]=cnt[id]+1; 
				scheme[j]=scheme[id];		//可以在松弛过程中更新属性值 
			}
			else if(dist[id]+g[id][j]==dist[j])
			{
				scheme[j]+=scheme[id];
				int h=happy[id]+city[j];
				double ave=h*0.1/(cnt[id]+1);
				if(h>happy[j] || (h==happy[j] && ave>happy[j]*0.1/cnt[j]))
				{
					path[j]=id;
					happy[j]=h;
					cnt[j]=cnt[id]+1;
				}
			}
		}  
    }
}

void outpath()
{
	vector<int>v;
	for(int i=dest;i;i=path[i]) v.push_back(i);
	cout<<index[0];
	for(int i=v.size()-1;i>=0;i--) cout<<"->"<<index[v[i]];
}

int main()
{
	string s1,s2;
    cin>>n>>m>>s1;
	n--;
    name[s1]=0;
    index[0]=s1;
    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= n; j++)
            g[i][j] = INF;
    for(int i=1;i<=n;i++)
    {
    	cin>>s1>>city[i];
    	name[s1]=i;
    	index[i]=s1;
    	if(s1=="ROM") dest=i;
	}
	for(int i=0,d;i<m;i++)
	{
		cin>>s1>>s2>>d;
		g[name[s1]][name[s2]]=g[name[s2]][name[s1]]=d;
	}
    dijkstra();
    cout<<scheme[dest]<<" "<<dist[dest]<<" "<<happy[dest]<<" "<<(int)happy[dest]/cnt[dest]<<endl;
    outpath();
    return 0;
}

你可能感兴趣的:(PAT甲级)