PAT A1087

1087 All Roads Lead to Rome (30分)
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 recommanded. 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 recommanded 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
  • 自做答案1:Dijkstra+DFS
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
using namespace std;

const int MAXV = 250;
const int INF = 1000000000;

int N,K;
string S,D;
map<string,int> stringToInt;
map<int,string> intToString;

int G[MAXV][MAXV];
bool vis[MAXV] = {false};

int weight[MAXV];

int d[MAXV];

int num[MAXV];

vector<int> pre[MAXV];
vector<int> tempPath,path;
int max_happy = 0;
double max_avgHappy = 0;

void Dijkstra(int s)
{
    fill(d,d + MAXV,INF);
    fill(num,num + MAXV,0);
    num[s] = 1;//**************
    d[s] = 0;

    for(int i = 0;i<N;i++)
    {
        int u = -1,MIN = INF;
        for(int j = 0;j<N;j++)
        {
            if(vis[j] == false && d[j] < MIN)
            {
                u = j;
                MIN = d[j];
            }
        }

        if(u == -1) return ;

        vis[u] = true;

        for(int v = 0;v<N;v++)
        {
            if(vis[v] == false && G[u][v]!= INF )
            {
                if( d[v] > d[u] + G[u][v])
                {
                    d[v] = d[u] + G[u][v];
                    num[v] = num[u];//num[v] = 1;
                    pre[v].clear();
                    pre[v].push_back(u);
                }
                else if(d[v] == d[u] + G[u][v])
                {
                    num[v] += num[u];
                    pre[v].push_back(u);
                }
            }
        }
    }
}

void DFS(int s,int v)
{
    int i;
    if(v == s)
    {
        tempPath.push_back(v);
        int happy = 0;
        double avgHappy = 0;
        for( i = tempPath.size()-1;i>=0;i--)
        {
            int id = tempPath[i];
            happy += weight[id];
            avgHappy += 1.0*weight[id]/(tempPath.size()-1);
        }

        if(happy > max_happy)
        {
            max_happy = happy;
            max_avgHappy = avgHappy;
            path = tempPath;
        }
        else if(happy == max_happy && avgHappy > max_avgHappy)
        {
            max_avgHappy = avgHappy;
            path = tempPath;
        }
        tempPath.pop_back();
        return ;
    }

    tempPath.push_back(v);
    for(i = 0;i<pre[v].size();i++)
    {
        DFS(s,pre[v][i]);
    }
    tempPath.pop_back();
}


int main()
{
    int i;
    cin>>N>>K>>S;
    D = "ROM";

    stringToInt[S] = 0;
    intToString[0] = S;
    weight[0] = 0;


    fill(G[0],G[0] + MAXV*MAXV,INF);//*******
    for(i = 1;i<=N-1;i++)
    {
        int w;
        string city;
        cin>>city>>w;
        stringToInt[city] = i;
        intToString[i] = city;
        weight[i] = w;
    }

    for(i = 1;i<=K;i++)
    {
        int u,v;
        int cost;
        string city1,city2;
        cin>>city1>>city2>>cost;
        u = stringToInt[city1];
        v = stringToInt[city2];
        G[u][v] = G[v][u] = cost;
    }

    int ints = stringToInt[S];
    int intd = stringToInt[D];
    Dijkstra(ints);
    DFS(ints,intd);
    printf("%d %d %d %d\n",num[intd],d[intd],max_happy,(int)max_avgHappy);

    for(i = path.size()-1;i>0;i--)
    {
        cout<<intToString[path[i]]<<"->";
    }
    cout<<intToString[path[0]]<<endl;

    return 0;
}

  • 注意点:
    将字符串用map和int关联起来
    然后是cost可以看成是G[][]的边

  • 自做答案2:Dijkstra
    涉及到第一标尺、第二标尺、第三标尺的问题

//北航复试2020/3/25
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
using namespace std;

const int MAXV = 250;
const int INF = 1000000000;

int N,K;
string S,D;
map<string,int> stringToInt;
map<int,string> intToString;


int G[MAXV][MAXV];
bool vis[MAXV] = {false};

int weight[MAXV];

int d[MAXV];
int num[MAXV];
int w[MAXV];

int pre[MAXV];//记录符合所有条件的一条最佳路径
int pt[MAXV];//记录符合所有条件的一条最佳路径上到达某一点的个数

void Dijkstra(int s)
{
    int i,j;
    fill(d,d+MAXV,INF);
    fill(num,num + MAXV,0);
    fill(w,w+MAXV,0);
    fill(pt,pt + MAXV,0);

    d[s] = 0;
    num[s] = 1;
    w[s] = weight[s];
    pt[s] = 0;
    for(i = 0;i<N;i++) pre[i] = i;

    for(i = 0;i<N;i++)
    {
        int u = -1,MIN = INF;
        for(j = 0;j<N;j++)
        {
            if(vis[j] == false && d[j] < MIN)
            {
                u = j;
                MIN = d[j];
            }
        }
        if(u == -1) return ;
        vis[u] = true;

        for(int v = 0;v<N;v++)
        {
            if(vis[v] == false && G[u][v] != INF)
            {
                if(d[v] > d[u] + G[u][v])
                {
                    d[v] = d[u] + G[u][v];
                    num[v] = num[u];
                    w[v] = w[u] + weight[v];
                    pt[v] = pt[u] + 1;
                    pre[v] = u;
                }
                else if(d[v] == d[u] + G[u][v])
                {
                    num[v] += num[u];
                    if(w[v] < w[u] + weight[v])
                    {
                        w[v] = w[u] + weight[v];
                        pt[v] = pt[u] + 1;
                        pre[v] = u;
                    }
                    else if(w[v] == w[u] + weight[v])
                    {
                        double avgu = 1.0*(w[u] + weight[v])/(pt[u] + 1);//经过u
                        double avgv = 1.0*w[v]/pt[v];//不经过u
                        if(avgu > avgv)
                        {
                            pt[v] = pt[u] + 1;
                            pre[v] = u;
                        }
                    }
                }
            }
        }
    }
}

void DFS(int s,int v)
{

    if(v==s)
    {
        cout<<intToString[v];
        return ;//**********造成一直打印的结果,没有return
    }


    DFS(s,pre[v]);
    cout<<"->"<<intToString[v];
}


int main()
{
    int i;
    cin>>N>>K>>S;
    D = "ROM";

    stringToInt[S] = 0;
    intToString[0] = S;
    weight[0] = 0;


    fill(G[0],G[0] + MAXV*MAXV,INF);//*******
    for(i = 1;i<=N-1;i++)
    {
        int w;
        string city;
        cin>>city>>w;
        stringToInt[city] = i;
        intToString[i] = city;
        weight[i] = w;
    }

    for(i = 1;i<=K;i++)
    {
        int u,v;
        int cost;
        string city1,city2;
        cin>>city1>>city2>>cost;
        u = stringToInt[city1];
        v = stringToInt[city2];
        G[u][v] = G[v][u] = cost;
    }

    int ints = stringToInt[S];
    int intd = stringToInt[D];

    Dijkstra(ints);
    printf("%d %d %d %d\n",num[intd],d[intd],w[intd],w[intd]/pt[intd]);
    DFS(ints,intd);
    cout<<endl;
    return 0;
}

发布了71 篇原创文章 · 获赞 36 · 访问量 9448

猜你喜欢

转载自blog.csdn.net/qq_34686440/article/details/105094575