All Roads Lead to Rome (Dijkstra路径保存DFS)

All Roads Lead to Rome (30)
  • 热度指数:5519时间限制:1秒空间限制:65536K
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.

输入描述:
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.


输出描述:
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".
示例1

输入

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

输出

3 3 195 97
HZH->PRS->ROM

分析

  输入城市数N,K条城市之间的连接,起点城市名称,后面N-1行为其余的城市名+其幸福值(起点城市无幸福值),再K行为城市间道路信息(City1,City2,Cost)。

  输出从起点城市到ROM城市的最低总Cost的方案数,最低总Cast,happiness,平均幸福值ave=总幸福值/经过城市数(起点城市不算)。

    再打印具体路径。

  思路:首先给城市编号,这里设置起点城市为0,终点为num["ROM"]。然后Dijkstra套路一下,最后DFS遍历每条最短路径,求出符合题意的

代码:

#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <stack>
using namespace std;
#define ll long long
const ll inf=0x3f3f3f3f3f3f3f3f;
ll N,K;
string Sp;//开始城市 
map<string,ll>num;//城市编号 
map<ll,string>num1;//城市编号 
ll hap[206];//城市幸福值 
ll d[206];
ll B[206];
vector<ll>fa[206];//父节点 

struct node{
    ll v,edge;
    node(ll _v,ll _e):v(_v),edge(_e){}
};
vector<node>G[40007];

struct TT{
    ll d,v;
    TT(ll _d,ll _v):d(_d),v(_v){}
    bool operator < (const TT& con) const{
        return d>con.d;
    }
};

void Dijkstra()
{
    priority_queue<TT>Q;
    for(ll i=0;i<N;i++) d[i]= (i==0?0:inf);
    Q.push(TT(0,0));
    while(!Q.empty())
    {
        TT x=Q.top();    Q.pop();
        if(B[x.v]) continue;
        B[x.v]=1;
        for(ll i=0;i<G[x.v].size();i++)//对每条边松弛 
        {
            ll u=x.v;    ll v=G[u][i].v;
            ll dis=G[u][i].edge;
            if(d[v]>(d[u]+dis)){
                d[v]=d[u]+dis;
                fa[v].clear();    fa[v].push_back(u);
                Q.push(TT(d[v],v));
            }
            else if(d[v]==(d[u]+dis)){
                fa[v].push_back(u);
                Q.push(TT(d[v],v));
            }
            
        }
    }
}
stack<ll>VV;
ll max_hap=-inf;//最高happiness 
ll ave;//最高hap所对应的平均hap
ll anss=0;//路径数 
void DFS(ll HAP,ll n,stack<ll>st,ll sum)
{
    HAP+=hap[n];
    st.push(n);
    sum++;
    if(n==0)//到源点 
    { 
        anss++;
        if(HAP>max_hap || (HAP==max_hap && HAP/(sum-1)>ave)){
            VV=st;
            max_hap=HAP;
            ave=HAP/(sum-1); 
        }
    }
    
    for(ll i=0;i<fa[n].size();i++)
    {
        DFS(HAP,fa[n][i],st,sum);
    }
}

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    cin>>N>>K>>Sp;
    num[Sp]=0;
    num1[0]=Sp; 
    for(ll i=1;i<N;i++) {
        string s;
        cin>>s;    
        num[s]=i;//编号 
        num1[i]=s;
        cin>>hap[i];
    }
    for(ll i=1;i<=K;i++)
    {
        string c1,c2;    int cost;
        cin>>c1>>c2>>cost;
        G[num[c1]].push_back(node(num[c2],cost));
        G[num[c2]].push_back(node(num[c1],cost));
    }
    Dijkstra();
    stack<ll>st1;
    DFS(0,num["ROM"],st1,0);
    cout<<anss<<" "<<d[num["ROM"]]<<" "<<max_hap<<" "<<ave<<endl;
    while(!VV.empty()){
        ll t=VV.top();    VV.pop();
        cout<<num1[t];
        if(VV.size()>0)    cout<<"->";
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/liuyongliu/p/11207718.html