1087. All Roads Lead to Rome (30)<最短路径,在最短路径上操作>

来源:互联网 发布:网易股票分时数据 编辑:程序博客网 时间:2024/05/01 05:35

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 HZHROM 100PKN 40GDN 55PRS 95BLN 80ROM GDN 1BLN ROM 1HZH PKN 1PRS ROM 2BLN HZH 2PKN GDN 1HZH PRS 1
Sample Output:
3 3 195 97HZH->PRS->ROM
分析:
这个题目用字符串作为数组的下标,所以1.用两个map实现字符串和数字索引的相互映射
2.利用dijkstra算法就最短路,在这个过程中,利用并查集记录父结点。当更新的时候发现有相等的情况时:路径条数相加,幸福感取最大的(更新分结点),如果幸福一样就取经过的城市少的(更新父结点),
当不相等的时候:更新结点上的幸福感、经过的城市数、传递路径条数(更新父结点)
#include<iostream>#include<string>#include<map>#include<vector>using namespace std;#define N 1000000typedef struct c{int happy;int count;}M;int main(){   //开始保存数据 int a,b,rom;string s;cin>>a>>b>>s;map<string ,int>stoi;//字符串与数字的相互映射 map<int,string>itos;stoi[s]=0;itos[0]=s;vector<vector<int> >arr(a,vector<int>(a,10000000));//数组储存 vector<int>happy(a);happy[0]=0;//记录每个城市的幸福度 for(int i=1;i<=a-1;i++){string s;int happ;cin>>s>>happ;stoi[s]=i;itos[i]=s;if(s=="ROM")rom=i;happy[i]=happ;}for(int i=0;i<b;i++){string s1,s2;cin>>s1>>s2;int cost;cin>>cost;arr[stoi[s1]][stoi[s2]]=cost;arr[stoi[s2]][stoi[s1]]=cost;}//数据保存完毕//求最少cost vector<int>dist(a);//rom到每个点的最近距离 vector<int>final(a,1);//记录收入集合的点 vector<M> hhh(a);//记录幸福感和与经过的城市数目的结点 for(int i=0;i<a;i++){dist[i]=arr[rom][i];//初始化 }dist[rom]=0;hhh[rom].happy=happy[rom];hhh[rom].count=0;vector<int>first(a,-1);//保存先人,利用并查集的思想 vector<int>flag(a,1);//记录路的条数 while(1){//找出下一个最小值;int min=N,min_;for(int i=0;i<a;i++){if(final[i]==1){if(dist[i]<min){min=dist[i];min_=i;//记录该城市 }}} if(min==N)break;//更新final[min_]=0;for(int i=0;i<a;i++){if(final[i]==1){int d=min+arr[min_][i];if(d<dist[i]){   dist[i]=d;first[i]=min_;//更新前驱 flag[i]=flag[min_];//传递路径  hhh[i].happy=hhh[min_].happy+happy[i];hhh[i].count=hhh[min_].count+1;}else if(d==dist[i]){  if(min!=0)flag[i]+=flag[min_];//计算路径 ,这是一个边界处理:当为第一个城市的时候,d就等于距离。他到其他城市的距离不应该加。 if(hhh[min_].happy+happy[i]>hhh[i].happy){hhh[i].happy=hhh[min_].happy+happy[i];hhh[i].count=hhh[min_].count+1;first[i]=min_;//更新前驱}else if(hhh[min_].happy+happy[i]==hhh[i].happy){if(hhh[min_].count+1<hhh[i].count){hhh[i].count=hhh[min_].count+1;first[i]=min_;//更新前驱}}}}}}cout<<flag[0]<<" "<<dist[0]<<" "<<hhh[0].happy<<" "<<hhh[0].happy/hhh[0].count;cout<<"\n";int a1=0;cout<<itos[0]<<"->";while(first[a1]!=rom){   a1=first[a1];cout<<itos[a1]<<"->";}cout<<itos[rom];return 0;} 


0 0
原创粉丝点击