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
评价:我最怕的就是我知道怎么做但是写不出来,这个是多指标的(居然有4个指标)最短路spfa算法的一般变式,因为里面变量很多,我对于写这样的题都是心有余而力不足,很担心会写错。虽然写完以后1Y了,但是也不能保证每次都行,我的代码能力还是偏弱的。
代码:
#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
int n,k;
string a[205];
int hap[205];
map<string,int> m;
struct node
{
int to;
int cost;
node(int _to,int _cost):to(_to),cost(_cost){}
};
vector<node> graph[205];
int dis[205];
int num[205];
int numOfp[205];
int h[205];
char inQ[205];
int Next[205];
void spfa(int x)
{
memset(dis,0x3f3f3f3f,sizeof(dis));
dis[x]=0;
Next[x]=-1;
num[x]=1;
numOfp[x]=0;
queue<int> que;
que.push(x);
inQ[x]=true;
h[x]=hap[x];
while(!que.empty())
{
int now=que.front();
que.pop();
inQ[now]=false;
for(int i=0;i<graph[now].size();i++)
{
int v=graph[now][i].to;
int c=graph[now][i].cost;
if(dis[v]>dis[now]+c)
{
dis[v]=dis[now]+c;
num[v]=num[now];
Next[v]=now;
numOfp[v]=numOfp[now]+1;
h[v]=h[now]+hap[v];
if(!inQ[v])
{
que.push(v);
inQ[v]=true;
}
}
else
if(dis[v]==dis[now]+c)
{
num[v]+=num[now];
if(h[v]<h[now]+hap[v])
{
h[v]=h[now]+hap[v];
Next[v]=now;
numOfp[v]=numOfp[now]+1;
if(!inQ[v])
{
que.push(v);
inQ[v]=true;
}
}
else
if(h[v]==h[now]+hap[v])
{
if(numOfp[v]>numOfp[now]+1)
{
numOfp[v]=numOfp[now]+1;
Next[v]=now;
if(!inQ[v])
{
que.push(v);
inQ[v]=true;
}
}
}
}
}
}
}
int main()
{
scanf("%d%d",&n,&k);
cin>>a[0];
m[a[0]]=0;
hap[0]=0;
int e;
for(int i=1;i<n;i++)
{
cin>>a[i]>>hap[i];
m[a[i]]=i;
if(a[i].compare("ROM")==0)
e=i;
}
string str1,str2;
int c;
while(k--)
{
cin>>str1>>str2>>c;
int x=m[str1];
int y=m[str2];
graph[x].push_back(node(y,c));
graph[y].push_back(node(x,c));
}
spfa(e);
printf("%d %d %d %d\n",num[0],dis[0],h[0],h[0]/numOfp[0]);
cout<<a[0];
int temp=Next[0];
while(temp!=-1)
{
cout<<"->"<<a[temp];
temp=Next[temp];
}
return 0;
}