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<stdio.h>
#include<map>
#include<string>
#include<string.h>
#include<stack>
using namespace std;
#define MAX 210
int INF = ;
int HappyVal[];
int visit[MAX];
int Grap[MAX][MAX];
int d[MAX];
int h[MAX];
int num[MAX];
int pre[MAX];
int Count[MAX]; void Dijkstra(int Begin,int NodeNum)
{
d[Begin] = ;
h[Begin] = HappyVal[Begin];
num[Begin] = ;
Count[Begin] = ;
for(int i = ;i < NodeNum ;i++)
{
int index = -;
int MIN = INF;
for(int j = ;j <NodeNum ;j++)
{
if(!visit[j] && d[j] < MIN)
{
index = j;
MIN = d[j];
}
} if(index == -) return ;
visit[index] = true;
for(int v = ;v <NodeNum ;v++)
{
if(!visit[v] && Grap[index][v]!=INF)
{
if(d[index]+Grap[index][v]<d[v])
{
d[v] = d[index]+Grap[index][v];
num[v] = num[index];
h[v] = h[index] + HappyVal[v];
pre[v] = index;
Count[v] = Count[index] +;
}
else if(d[index]+Grap[index][v]==d[v])
{
num[v] = num[v] + num[index]; if(h[v] < h[index] + HappyVal[v])
{
h[v] = h[index] + HappyVal[v];
Count[v] = Count[index] +;
pre[v] = index;
}
else if( h[v] == h[index] + HappyVal[v] && (double)(h[index] + HappyVal[v])/(Count[index]+) > (double)h[v]/Count[v])
{
Count[v] = Count[index] +;
pre[v] = index;
}
}
}
}
} } int main()
{
int i,j,N,K,happy,ROM;
char Begin[],tem[];
scanf("%d%d%s",&N,&K,Begin);
map<string,int> mm;
map<int,string> mm2;
mm[Begin] = ;
mm2[] = Begin ;
HappyVal[mm[Begin]] = ;
for(i = ; i < N ;i++)
{
scanf("%s%d",tem,&happy);
if(strcmp("ROM",tem)==) ROM = i;
mm[tem] = i;
mm2[i] = tem;
HappyVal[i] = happy;
} char x[],y[]; for(i = ; i < N ;i++)
{
for(j = ; j < N ;j++)
{
Grap[i][j] = INF;
}
d[i] = h[i] = INF;
pre[i] = -;
Count[i] = ;
} for(i = ; i < K ;i++)
{
scanf("%s%s",x,y);
scanf("%d",&Grap[mm[x]][mm[y]]);
Grap[mm[y]][mm[x]] = Grap[mm[x]][mm[y]];
} Dijkstra( mm[Begin] , N); printf("%d %d %d %d\n",num[mm["ROM"]],d[mm["ROM"]],h[mm["ROM"]],h[mm["ROM"]]/Count[mm["ROM"]]); stack<int> ss;
i= mm["ROM"];
while(i != -)
{
ss.push(i);
i = pre[i];
}
int fir = ;
while(!ss.empty())
{
if(fir == )
{
fir = ;
printf("%s",mm2[ss.top()].c_str());
}
else printf("->%s",mm2[ss.top()].c_str());
ss.pop();
} printf("\n"); return ;
}
上一篇:linux Cron 执行Django 任务计划


下一篇:[转]Oracle SOME,ANY,All,EXISTS,IN