【PAT 1002 All Roads Lead to Rome】+ dfs + dijkstra

12 篇文章 0 订阅
12 篇文章 0 订阅

All Roads Lead to Rome (30)
时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)
题目描述
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”.

输入例子:
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

题意 : 找出 s 到 ROM 的最短路,最短路相同时, 选幸福值和大的路径,最短路径和幸福值都相同时,选平均幸福值大的,
输出 和最短路径相同的路径数,最短路径,最大幸福值,平均幸福值

思路 : dijkstra 更新到 ROM 的所有最优路径,dfs 按照 最短路径,最大幸福值,平均幸福值 的优选级更新最优解

AC代码:

#include<cstdio>
#include<map>
#include<vector>
#include<cmath>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX = 210;
const int INF = 0x3f3f3f3f / 2;
typedef long long LL;
typedef pair <int,int> p;
int d[MAX],vis[MAX],xf[MAX],n,m,c,ans,sum,cut,dis[MAX][MAX];
struct node{
    int t,vl;
};
map <string,int> ma;
map <int,int> mm;
vector <int> vv[MAX],vl,vll;
vector <node> v[MAX];
string s,ss[MAX],a,b;
void dj(){
    priority_queue<p, vector <p> ,greater<p> > q;
    fill(d,d + n,INF);
    d[ma["ROM"]] = 0;
    q.push(p(0,ma["ROM"]));
    while(!q.empty()){
        p w = q.top(); q.pop();
        int u = w.second;
        if(d[u] < w.first) continue;
        for(int i = 0; i < v[u].size(); i++){
            node z = v[u][i];
            if(d[z.t] > d[u] + z.vl){
                d[z.t] = d[u] + z.vl;
                vv[z.t].clear();
                vv[z.t].push_back(u);
                q.push(p(d[z.t],z.t));
            }
            else if(d[z.t] == d[u] + z.vl)
                vv[z.t].push_back(u);
        }
    }
}

void dfs(int x){
    vll.push_back(x);
    if(x == ma["ROM"]){
        int h = 0,l = 0;
        for(int i = 1; i < vll.size(); i++)
            h += xf[vll[i]],l += dis[vll[i]][vll[i - 1]];
        mm[l]++;
        if(l < cut) cut = l,ans = h,sum = h / (vll.size() - 1),vl = vll;
        else if(l == cut && h > ans) ans = h,sum = h / (vll.size() - 1),vl = vll;
        else if(l == cut && h == ans && sum < h / (vll.size() -1)) sum = h / (vll.size() - 1),vl = vll;
        return ;
    }
    for(int i = 0; i < vv[x].size(); i++)
        dfs(vv[x][i]),vll.pop_back();
}
int main()
{
    cin >> n >> m >> s;
    ma[s] = 0;
    ss[0] = s;
    for(int i = 1; i < n; i++)
        cin >> ss[i] >> xf[i] ,ma[ss[i]] = i;
    while(m--){
        cin >> a >> b >> c;
        int x = ma[a],y = ma[b];
        dis[x][y] = dis[y][x] = c;
        node o;
        o.t = y,o.vl = c;
        v[x].push_back(o);
        o.t = x;
        v[y].push_back(o);
    }
    dj();
    ans = -INF,sum = -INF,cut = INF;
    vll.clear();
    mm.clear();
    dfs(ma[s]);
    printf("%d %d %d %d\n",mm[cut],cut,ans,sum);
    for(int i = 0; i < vl.size() - 1; i++)
        cout << ss[vl[i]] << "->";
    puts("ROM");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值