PAT (Advanced Level) 1087 All Roads Lead to Rome

题解

    最短路径经典题型。套最短路的板子再加上额外的要求就可以了(说起来好简单)。SPFA也行,Dijkstra也可以。这里我用的是SPFA。因为题目要求,将地名和其对应的数字用map映射一下,这样方便处理。

same[i]代表到达地点 i 有几种路径;

dist[i]代表从起点到地点 i 的最短距离;

happy[i]代表从起点到地点 i 的幸福值;

cnt[i]代表从起点到地点 i 需要经过几个城市;

ans[i]代表从哪个地点到达了地点 i ;

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn=250,inf=0x3fffffff;
struct node
{
    int v,w;
    node(int a,int b)
    {v=a;w=b;}
};
int same[maxn]={0,1},dist[maxn],vis[maxn],happy[maxn],cnt[maxn],ans[maxn];
int value[maxn],N,K;
map<string,int> smapi;
map<int,string> imaps;
vector<node> edge[maxn];
void spfa(int s);
void print(int p);
int main()
{
    string str_s,str,u,v;
    int i,w,end;
    scanf("%d%d",&N,&K);
    cin>>str_s;
    smapi[str_s]=1; imaps[1]=str_s;

    for(i=2;i<=N;i++)
    {
        cin>>str>>w;
        if(str=="ROM") end=i;
        smapi[str]=i; imaps[i]=str;
        value[i]=w;
    }

    for(i=0;i<K;i++)
    {
        cin>>u>>v>>w;
        edge[smapi[u]].push_back(node(smapi[v],w));
        edge[smapi[v]].push_back(node(smapi[u],w));
    }

    spfa(1);

    printf("%d %d %d %d\n",same[end],dist[end],happy[end],happy[end]/cnt[end]);
    print(ans[end]);
    printf("ROM");

    system("pause");
    return 0;
}
void print(int p)
{
    if(p==0) return ;
    print(ans[p]);
    printf("%s->",imaps[p].c_str());
}
void spfa(int s)
{
    queue<int> que;
    int p,i,v,w;

    fill(dist+2,dist+maxn,inf);
    que.push(s);
    while(!que.empty())
    {
        p=que.front();que.pop();
        vis[p]=0;
        for(i=0;i<edge[p].size();i++)
        {
            v=edge[p][i].v;
            w=edge[p][i].w;

            if(dist[v]>dist[p]+w)
            {
                dist[v]=dist[p]+w;
                happy[v]=happy[p]+value[v];
                cnt[v]=cnt[p]+1;
                ans[v]=p;
                same[v]=same[p];
                if(!vis[v])
                {
                    que.push(v);
                    vis[v]=1;
                }
            }
            else if(dist[v]==dist[p]+w)
            {
                same[v]+=same[p];
                if(happy[v]<happy[p]+value[v])
                {
                    happy[v]=happy[p]+value[v];
                    cnt[v]=cnt[p]+1;
                    ans[v]=p;
                }
                else if(happy[p]+value[v]==happy[v])
                {
                    if(cnt[v]>cnt[p]+1)
                    {
                        cnt[v]=cnt[p]+1;
                        ans[v]=p;
                    }
                }
            }
        }
    }
}

原文地址:https://www.cnblogs.com/VividBinGo/p/12231705.html

时间: 2024-08-03 17:58:05

PAT (Advanced Level) 1087 All Roads Lead to Rome的相关文章

PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace st

Pat(Advanced Level)Practice--1087(All Roads Lead to Rome)

Pat1087代码 题目描述: 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. F

1087. All Roads Lead to Rome (30)【最短路】——PAT (Advanced Level) Practise

题目信息 1087. All Roads Lead to Rome (30) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B 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. I

[PAT]1087. 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 suppos

PAT 1087 All Roads Lead to Rome

1 #include <cstdio> 2 #include <climits> 3 #include <iostream> 4 #include <vector> 5 #include <string> 6 #include <queue> 7 #include <unordered_map> 8 #include <algorithm> 9 10 using namespace std; 11 12 typ

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 Spec

pat1087. 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 gain

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes