1087 (两种方法)All Roads Lead to Rome--条条道路通罗马

13 篇文章 0 订阅

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

题意:有N个城市,K条路,每个城市有幸福值(除了起点),现在给出起点和终点ROM,输出最短路径条数,最短距离,幸福值之和和平均幸福值(只输出整数部分),在另一行输出路径。如果路径不唯一,选择路径上幸福值之和最大的那条,如果还不唯一,输出平均幸福值最大的那条。

有两种思路:
1.Dijkstra+DFS,Dijkstra找出所有最短路径(第一标尺),DFS求出幸福值之和,平均幸福值(第二标尺)。
2.只使用Dijkstra,这样就需要记录顶点个数来求平均幸福值,开一个数组pt[maxn]表示最短路径上的顶点数,每当以u为中介点能使得v中的某个数据优化时,pt[v]就赋值pt[u]+1。
注意点:
1、没有强制顶点的输入范围,可以把起点设为0号顶点,其余顶点设为1~n-1号顶点;
2、城市名和编号的对应可以用map,也可以用字符串hash;
3、顶点没有点权,因此计算平均点权时是不计算起点的,pt也应该初始化为0。

if(vis[v] == false && G[u][v] != INF)
{
    if(G[u][v] + d[u] < d[v])
    {
        num[v] = num[u];
        d[v] = G[u][v] + d[u];
        w[v] = w[u] + weight[v];
        pt[v] = pt[u] + 1;
        pre[v] = u;
    }
    else if(G[u][v] + d[u] == d[v]) //找到相同长度的路径
    {
        num[v] += num[u];       //到v的最短路径条数继承自num[u]
        if(w[u] + weight[v] > w[v])
        {
            w[v] = w[u] + weight[v];
            pt[v] = pt[u] + 1;  //s->v顶点个数等于s->u顶点个数+1
            pre[v] = u;
        }
        else if(w[u] + weight[v] == w[v])
        {
            double uavg = 1.0 * (w[u] + weight[v]) / (pt[u] + 1);   
            double vavg = 1.0 * w[v] / pt[v];
            if(uavg > vavg)     //以u为中介点使平均点权更大
            {
                pt[v] = pt[u] + 1;  //s->v顶点个数等于s->u顶点个数+1
                pre[v] = u;
            }
        }
    }
}

Dijkstra+DFS:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 410;
const int INF = 100000;
int n, st, m, G[maxn][maxn];
int d[maxn], weight[maxn], w[maxn], num[maxn];
bool vis[maxn] = {false};
vector<int> pre[maxn], tempPath, path;
map<string, int> CitytoIndex;   //城市名转换成编号
map<int, string> IndextoCity;   //编号转换成城市名
int maxW = 0;   //最大权重
double maxAvg = 0;  //最大平均点权

void Dijkstra(int s)
{
    fill(d, d + maxn, INF);
    fill(num, num + maxn, 0);
    fill(w, w + maxn, 0);
    d[s] = 0;
    num[s] = 1;
    w[s] = weight[s];
    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(G[u][v] + d[u] < d[v])
                {
                    d[v] = G[u][v] + d[u];
                    num[v] = num[u];
                    pre[v].clear();
                    pre[v].push_back(u);
                }
                else if(G[u][v] + d[u] == d[v])
                {
                    num[v] += num[u];
                    pre[v].push_back(u);
                }
            }
        }
    }
}


void dfs(int v)
{
    if(v == st)     //注意,虽然没有对st赋值,但是全局变量默认初始化为0
    {
        tempPath.push_back(v);
        int tempW = 0;
        for(int i = tempPath.size() - 2; i >= 0; i--)   //起点没有权重,所以从tempPath.size() - 2开始
        {
            int id = tempPath[i];
            tempW += weight[id];
        }
        double tempAvg = 1.0 * tempW / (tempPath.size() - 1);   //总顶点数为tempPath.size() - 1
        if(tempW > maxW)
        {
            maxW = tempW;
            maxAvg = tempAvg;
            path = tempPath;
        }
        else if(tempW == maxW && tempAvg > maxAvg)
        {
            maxAvg = tempAvg;
            path = tempPath;
        }
        tempPath.pop_back();
        return;
    }
    tempPath.push_back(v);
    for(int i = 0; i < pre[v].size(); i++)
        dfs(pre[v][i]);
    tempPath.pop_back();
}




int main()
{
    fill(G[0], G[0] + maxn * maxn, INF);
    string city1, city2, start;
    cin >> n >> m >> start;
    CitytoIndex[start] = 0;
    IndextoCity[0] = start;
    for(int i = 1; i < n; i++)
    {
        cin >> city1 >> weight[i];
        CitytoIndex[city1] = i;
        IndextoCity[i] = city1;
    }
    for(int i = 0; i < m; i++)
    {
        cin >> city1 >> city2;
        int c1 = CitytoIndex[city1], c2 = CitytoIndex[city2];
        cin >> G[c1][c2];
        G[c2][c1] = G[c1][c2];
    }
    Dijkstra(0);
    int rom = CitytoIndex["ROM"];	
    dfs(rom);
    printf("%d %d %d %d\n", num[rom], d[rom], maxW, (int)maxAvg);	//注意maxAvg只取整数部分
    
    for(int i = path.size() - 1; i >= 0; i--)
    {
        cout << IndextoCity[path[i]];
        if(i > 0)   cout << "->";
    }
	return 0;
}

只使用Dijkstra

#include<bits/stdc++.h>
using namespace std;
const int maxn = 410;
const int INF = 100000;
int n, st, m, G[maxn][maxn];
int d[maxn], weight[maxn], w[maxn], num[maxn];
bool vis[maxn] = {false};
int pre[maxn];
map<string, int> CitytoIndex;
map<int, string> IndextoCity;
int pt[maxn];   //最短路径的顶点数

void Dijkstra(int s)
{
    fill(d, d + maxn, INF);
    fill(num, num + maxn, 0);
    fill(w, w + maxn, 0);
    fill(pt, pt + maxn, 0);     //初始化为0
    d[s] = 0;
    num[s] = 1;
    w[s] = weight[s];
//    pt[s] = 1;        //出错
//    for(int i = 0; i < n; i++)    //可不写
//        pre[i] = i;
    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(G[u][v] + d[u] < d[v])
                {
                    d[v] = G[u][v] + d[u];
                    w[v] = w[u] + weight[v];
                    pt[v] = pt[u] + 1;
                    pre[v] = u;
                    num[v] = num[u];
                }
                else if(G[u][v] + d[u] == d[v])
                {
                    num[v] += num[u];
                    if(w[u] + weight[v] > w[v])
                    {
                        w[v] = w[u] + weight[v];
                        pt[v] = pt[u] + 1;
                        pre[v] = u;
                    }
                    else if(w[u] + weight[v] == w[v])
                    {
//                        double uavg = 1.0 * w[u] / pt[u];
//                        double vavg = 1.0 * (w[u] + weight[v]) / (pt[u] + 1);
//                        double vavg = 1.0 * w[v] / pt[v];
                        double uavg = 1.0 * (w[u] + weight[v]) / (pt[u] + 1);
                        double vavg = 1.0 * w[v] / pt[v];
                        if(uavg > vavg)
                        {
                            pt[v] = pt[u] + 1;
                            pre[v] = u;
                        }
                    }
                }
            }
        }
    }
}

void printPath(int v)
{
    if(v == st)
    {
        cout << IndextoCity[v];     //v是整数,要输出的是名称,所以输出IndextoCity[v]
        return;
    }
    printPath(pre[v]);
    cout << "->" << IndextoCity[v];
}

int main()
{
    fill(G[0], G[0] + maxn * maxn, INF);
    string city1, city2, start;
    cin >> n >> m >> start;
    CitytoIndex[start] = 0;
    IndextoCity[0] = start;
    for(int i = 1; i < n; i++)
    {
        cin >> city1 >> weight[i];
        CitytoIndex[city1] = i;
        IndextoCity[i] = city1;
    }
    for(int i = 0; i < m; i++)
    {
        cin >> city1 >> city2;
        int c1 = CitytoIndex[city1], c2 = CitytoIndex[city2];
        cin >> G[c1][c2];
        G[c2][c1] = G[c1][c2];
    }
    Dijkstra(0);
    int rom = CitytoIndex["ROM"];
    printf("%d %d %d %d\n", num[rom], d[rom], w[rom], w[rom] / pt[rom]);
    printPath(rom);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值