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.

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

解题思路

dijkstra算法

AC代码

#include <iostream>
#include <stdio.h>
#include <string>
#include <map>
#include <vector>

using namespace std;
#define MAX 66666666
#define SIZE 203

vector<int> resPath;
int routeSum=0,shortest = MAX,resHap=0,avgHap = 0;
map<string,int> hapiness1;
map<int,string> hapiness2;
map<int,int> hapSet;
int graph[SIZE][SIZE];
int dij[SIZE];
int visit[SIZE];
int n,k;
void dfs(int start,int end,vector<int> &path,int hapSum,int pathSum);
void dijkstra(int s);

int main()
{
    int i,j,index1,tmp1,tmp2,tmp3;
    int dest = 0;
    string city1,city2,city3,city4;
    cin>>n>>k>>city1;
    index1= 0;
    hapiness1[city1] = index1;
    hapiness2[index1] = city1;
    hapSet[index1] = 0;
    for(i=1;i<n;i++)
    {
        cin>>city2>>tmp1;
        if(city2.compare("ROM")==0)
        {
            dest = i;
        }
        hapiness1[city2] = i;
        hapiness2[i] = city2;
        hapSet[i] = tmp1;
    }
    for(i=0;i<n;i++)
    {
        dij[i] = MAX;
        for(j=0;j<n;j++)
        {
            graph[i][j] = MAX;
        }
    }
    for(i=0;i<k;i++)
    {
        cin>>city2>>city3>>tmp1;
        tmp2 = hapiness1[city2];
        tmp3 = hapiness1[city3];
        if(tmp1<graph[tmp2][tmp3])
        {
            graph[tmp2][tmp3] = tmp1;
            graph[tmp3][tmp2] = tmp1;
        }
    }

    dijkstra(0);
    for(i=0;i<n;i++)
    {
        visit[i] = 0;
    }
    shortest = dij[dest];
    vector<int> myPath(1,0);
    visit[0] = 1;
    dfs(0,dest,myPath,0,0);
    printf("%d %d %d %d\n",routeSum,shortest,resHap,avgHap);
    cout<<city1;
    for(i=1;i<resPath.size();i++)
    {
        cout<<"->"<<hapiness2[resPath[i]];
    }
    cout<<endl;
    return 0;
}

void dfs(int start,int end,vector<int> &path,int hapSum,int pathSum)
{
    int i,j;
    if(pathSum>shortest)
    {
        return ;
    }
    if(start==end)
    {
        if(pathSum>shortest)
        {
            return ;
        }
        routeSum ++;
        if(hapSum<resHap)
        {
            return ;
        }
        int ta = hapSum/(path.size()-1);
        if(ta>avgHap)
        {
            resPath = path;
            avgHap = ta;
            resHap = hapSum;
        }
        return ;
    }
    for(i=0;i<n;i++)
    {
        if(!visit[i] && graph[start][i]!=MAX)
        {
            path.push_back(i);
            visit[i] = 1;
            dfs(i,end,path,hapSum + hapSet[i],pathSum+graph[start][i]);
            path.pop_back();
            visit[i] = 0;
        }
    }
}

void dijkstra(int s)
{
    int i,j,tmp,so;
    for(i=0;i<n;i++)
    {
        dij[i] = graph[s][i];
        visit[i] = 0;
    }
    dij[s] = 0;
    visit[s] = 1;

    for(i=1;i<n;i++)
    {
        tmp = MAX;
        so = s;
        for(j=0;j<n;j++)
        {
            if(!visit[j] && dij[j]<tmp)
            {
                tmp = dij[j];
                so = j;
            }
        }
        visit[so] = 1;
        for(j=0;j<n;j++)
        {
            if(!visit[j] && dij[so]+graph[so][j]<dij[j])
            {
                dij[j] = dij[so]+graph[so][j];
            }
        }
    }

}
时间: 2024-08-28 10:12:27

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

[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

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

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

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 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

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

1014. Waiting in Line (30)——PAT (Advanced Level) Practise

题目信息: 1014. Waiting in Line (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rule

1004. Counting Leaves (30)——PAT (Advanced Level) Practise

题目信息: 1004. Counting Leaves (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contai

1068. Find More Coins (30)【背包】——PAT (Advanced Level) Practise

题目信息 1068. Find More Coins (30) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins a