pat 1087. All Roads Lead to Rome (30)

pat 1087. All Roads Lead to Rome (30)

时间限制
200 ms
内存限制
32000 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

今天又进行了一次pat甲级考试,对于这次pat考试,我只能说,悔恨不已。我已经练习了很多题目,前面的题目,我都很容易就过了。虽然不是很快,我前三道题目,只有第二道题目的第一次编译没通过,因为当时换电脑,怕丢失,就急急慢慢提交了一次。其实我那个流程全部都是对的。可是第四道题目来了,传说中最难的题目,我感觉有点不知所措,我看了题目之后,想出了思路,然后整个流程都是对的。字符串和数字互换,求最短路径,然后求最短路径当中最优的路径。可惜我看错了题目,其中有一道题目需要输出最短路径的条数,我一直看不懂 the number of different routes with the least cost 是啥意思,第一次我理解了,是有多少条路径,可是我做的时候就忘记了,理解成了最优解当中,有多少个城市,唉。最开始都把这个忽视了,以后对于输入输出一定要小心的看。

总的来说,这次我就是在审题上出错了,以后对于输入输出一定要小心看清楚,编写代码。虽然pat考察的点比较固定,但是每次输入输出都是有变化的。所以就要小心。这次我考了85分,应该来说对比上次不到四十分,算比较好了。可是还不甘心,感觉人应该知足了。哈哈!

下面上代码:

经典的求最短路径当中,满足条件的最短路径。我一半用dijkstra+dfs+剪枝,代码会比较长。有人直接用dfs做也可以,一边搜素,一边修改。


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

using namespace std;
#define MAX 66666666
#define SIZE 203
// output: number of routes,lowest cost, happiness,avgHap
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;
        }
    }
}
//6:52 ->6:58
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];
            }
        }
    }
    
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值