P1087. All Roads Lead to Rome (30)

来源:互联网 发布:微博域名格式 编辑:程序博客网 时间:2024/05/02 10:48

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

这个题大概意思是,先求出从起点到终点ROM的最短路,可能有多条,其中每个路径节点都有一个快乐值,求有多少条最短路,路径快乐值最大是多少,路径快乐平均值最大是多少。
因为题目的输入全部是字符串,所以我们用一个map映射我们自己规定的字符串和序号的映射。然后,用dijkstra算法来求得最短路并且对于每一个节点用vector来保存上一个节点。由于每个节点的父节点可能有多个,我们在执行完最短路算法之后再进行深度优先搜索,利用记录下来的last来访问前一个节点,最后到起始节点0的时候计算快乐值就ok了。
整个题目并不复杂,但是用到的东西很多,需要胆大心细的去用。
我运气不错,只调了2次就过了。
第一次题目看走眼了,快乐平均值是除去罗马的快乐值之外总快乐除以除去罗马之外总节点个数,我说样例怎么是97而我算得98来着。
#include <iostream>#include <cstring>#include <vector>#include <algorithm>#include <cstdio>#include <map>using namespace std;map<int,string>dsa;map<string,int> asd;int grond[201][201];int flag[201];int dist[201];int val[201];vector<int> last[201];vector<int> rem;int m,n,fin;double ans,pin;int num1;void dfs(int s,vector<int> d){    if(s == 0){        num1++;        int len = d.size();        double www = 0;        for(int i = 0;i<len;i++){            www += val[d[i]];        }        if(www == ans){            if(www/len > pin){                ans=www;                pin = www/(len-1);                rem = d;            }        }        else if(www > ans)        {            ans=www;            pin =  www/(len-1);            rem = d;        }    }    int len = last[s].size();    if(len<1)        return;    for(int i =0 ;i<len;i++){        if(!flag[last[s][i]]){            flag[last[s][i]]=1;            d.push_back(last[s][i]);            dfs(last[s][i],d);            d.pop_back();            flag[last[s][i]]=0;        }    }}void dis(){    memset(flag,0,sizeof(flag));    dist[0]=0;    flag[0]=1;    for(int i =1;i<n;i++){        dist[i]=grond[0][i];        if(dist[i]!=9999)            last[i].push_back(0);    }    for(int i = 1;i<n;i++){        int k = -1;        for(int j = 1;j<n;j++){            if(!flag[j] && (k==-1 || dist[k]>dist[j]))                k=j;        }        if(k==-1)            break;        flag[k]=1;        for(int j = 0;j<n;j++){            if(!flag[j] && dist[j]>dist[k]+grond[k][j]){                dist[j]=dist[k]+grond[k][j];                last[j].clear();                last[j].push_back(k);            }            else if(!flag[j] && dist[j]==dist[k]+grond[k][j]){                last[j].push_back(k);            }        }    }    ans = 0;    pin = 0;    memset(flag,0,sizeof(flag));    int now = fin;    int v = val[fin];    vector<int> sd;    sd.push_back(fin);    num1 = 0;    dfs(now,sd);    int lll = rem.size();    int ppppp = (int)pin;    cout<<num1<<" "<<dist[fin]<<" "<<ans<<" "<<ppppp<<endl;    for(int i = lll-1;i>0;i--){        cout<<dsa[rem[i]]<<"->";    }    cout<<dsa[rem[0]]<<endl;}int main(){    string s;    cin>>n>>m>>s;    asd[s]=0;    dsa[0]=s;    int tem;    for(int i = 1;i<n;i++){        cin>>s>>tem;        if(s=="ROM")            fin = i;        asd[s]=i;        dsa[i]=s;        val[i]=tem;    }    string d;    memset(grond,9999,sizeof(grond));    for(int i =0;i<m;i++){        cin>>s>>d>>tem;        int aa = asd[s];        int bb = asd[d];        grond[aa][bb]=tem;        grond[bb][aa]=tem;    }    dis();    return 0;}