P1087. All Roads Lead to Rome (30)

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

這個題大概意思是,先求出從起點到終點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;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章