1087 All Roads Lead to Rome (30分)
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 recommanded. 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 recommanded 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
题目
结点权值:每个城市的快乐度
边权值:每条路径的花费
统计边数:统计最短路径经过的结点的个数
最短路径个数:统计最短路径有几条
保存路径:保存路径,后续输出
上述分别需要维护一个列表;
int D[MAXcity]; //最少花费
int mostHap[MAXcity]; //最多快乐度
int Count[MAXcity]; //最短路径的个数
int C[MAXcity]; //统计路程经过的边数
int path[MAXcity]; //记录路径
最少最短都初始化为INFINITY;
统计个数边数都初始化为0;
拓展Dijkstra算法:
void Dijkstra()
{
bool visited[MAXcity];
fill(D,D+MAXcity,INFINITY); //最短距离
fill(visited,visited+MAXcity,false);
fill(mostHap,mostHap+MAXcity,0);
fill(Count,Count+MAXcity,0);
fill(C,C+MAXcity,0);
D[0]=0; //自己到自己花钱0
Count[0]=1; //自己到自己有一条
path[0]=-1; //设置开头-1,方便后续入遍历输出
//
while(1)
{
int Min=INFINITY;
int V=-1;
for(int i=0;i<N;i++)
if(!visited[i]&&D[i]<Min)
{
Min=D[i];
V=i;
}
if(V==-1) break;
visited[V]=true;
for(int i=0;i<N;i++)
{
if(!visited[i]){
if(D[V]+Graph[V][i]<D[i])
{
D[i]=D[V]+Graph[V][i];
mostHap[i]=mostHap[V]+happiness[i];
Count[i]=Count[V];
C[i]=C[V]+1;
path[i]=V;
}
else if(D[V]+Graph[V][i]==D[i]){
//这里的else很关键——小于后D[i]改变,可能满足后序的==
Count[i]+=Count[V];
if(mostHap[i]<mostHap[V]+happiness[i])
{
D[i]=D[V]+Graph[V][i];
mostHap[i]=mostHap[V]+happiness[i];
path[i]=V;
C[i]=C[V]+1;
}
}
}
}
}
}
注意点
判断相等一定要加else if;
因为前面的小于条件会改变D【i】的值,可能再次进入==判断;
难点
对每个列表的维护;
要求数最短路径有多少条
count[origin] = 1;
如果找到更短路:count[i]=count[V];因为W与V只差一条边,所以两者相同;
如果找到等长路:count[i]+=count[V];登场路的数量为W前面一个点的路的数量;
要求边数最少的最短路
C[origin] = 0;
如果找到更短路:C[i]=C[V]+1;
如果找到等长路:C[i]=C[V]+1;
要求最短路径的快乐度最高
mostHap[orrgin]=0;
如果找到更短路:mostHap[i]=mostHap[V]+happiness[i]
如果找到等长路:if(mostHap[V]+happiness[i]>mostHap[i])
mostHap[i]=mostHap[V]+happiness[i];
得到路径
每个条件都加上 path[i]=V;
最后通过path[]可以一路找到出发点;
输入方法:
没有用map,开names数组保存名字;
开happiness数组保存每一个城市的快乐度;
void input()
{
cin>>N>>K>>names[0]; //名称
happiness[0]=0; //幸福感
for(int i =1;i<N;i++)
{
cin>>names[i]>>happiness[i];
if(names[i]=="ROM") Rom=i;
}
for(int i=0;i<K;i++)
{
string a,b;
int x,y,cost;
cin>>a>>b>>cost;
x=Findname(names,a);
y=Findname(names,b);
Graph[x][y]=cost;
Graph[y][x]=cost;
}
}
用到的findname函数是一个简单的查找序号;
用散列表map可以更方便的实现,但是我没有用;
int Findname(string names[],string name)
{
for(int i=0;i<N;i++)
{
if(names[i]==name)
return i;
}
}
头文件以及main函数
#include<iostream>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
#define INFINITY 65533
#define MAXcity 201
string names[MAXcity];
int happiness[MAXcity];
int Graph[MAXcity][MAXcity]; //记得初始化为正无穷
int N,K;
string start;
int Rom;
int main()
{
fill(Graph[0],Graph[0]+MAXcity*MAXcity,INFINITY);
input();
//得到图,名称,幸福感列表;
//求0到各点的最少钱,并加上幸福感
//若最少钱相等,选幸福感最大的路线
//若还相等——平均幸福感最大的——即幸福感除以路过的城市数
Dijkstra();
cout<<Count[Rom]<<" "<<D[Rom]<<" "<<mostHap[Rom]<<" "<<mostHap[Rom]/C[Rom]<<endl;
stack<int> P;
int i=Rom;
P.push(i);
while(path[i]!=-1)
{
i=path[i];
P.push(i);
}
while(!P.empty())
{
int t=P.top();
P.pop();
if(t!=Rom)
cout<<names[t]<<"->";
else cout<<names[t];
}
}