PAT1087. All Roads Lead to Rome

PAT1087. All Roads Lead to Rome

题目大意

给定一个图的边权和点权, 求边权最小的路径; 若边权相同, 求点权最大; 若点权相同, 则求平均点权最大.

思路

先通过 Dijkstra 求得最短路径, 需要注意的是: 要保证每次松弛时 u 和 v 不相同, 否则会形成自环, 则从 ROM 开始 BFS 遍历每一条边权相同的路径.

代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
using namespace std;
#define MAXN 300
#define INF 0x7ffffff
int nVertex, nEdge;
map<string, int> s_i;
map<int, string> i_s;
vector<int> prepath[MAXN], temppath, anspath;
int vw[MAXN];
int ew[MAXN][MAXN];
int dis[MAXN];
int isVis[MAXN];
int cntRo = 0;
int ansHapy = 0;
double ansAvg = 0;
void dfs(int loc){
    temppath.push_back(loc);
    if(loc == 0){
        int hapy = 0;
        for(int i = 0; i < temppath.size(); i++)
            hapy += vw[temppath[i]];
        double avgHapy = hapy * 1.0 / (temppath.size() - 1);
        if(hapy > ansHapy){
            ansHapy = hapy;
            ansAvg = avgHapy;
            anspath = temppath;
        }
        else if(hapy == ansHapy && avgHapy > ansAvg){
            ansAvg = avgHapy;
            anspath = temppath;
        }
        cntRo++;
        temppath.pop_back();
        return;
    }
    for(int i = 0; i < prepath[loc].size(); i++){
        dfs(prepath[loc][i]);
    }
    temppath.pop_back();
}
int main(){
    scanf("%d%d", &nVertex, &nEdge);
    string tempStr; cin >> tempStr;
    s_i[tempStr] = 0; i_s[0] = tempStr;
    for(int i = 1; i < nVertex; i++){
        cin >> tempStr;
        s_i[tempStr] = i; i_s[i] = tempStr;
        scanf("%d", &vw[i]);
    }
    for(int i = 0; i < MAXN; i++){
        for(int j = 0; j < MAXN; j++){
            ew[i][j] = (i == j ? 0 : INF);
        }
    }
    for(int i = 0; i < nEdge; i++){
        string a, b; int c;
        cin >> a >> b >> c;
        ew[s_i[a]][s_i[b]] = ew[s_i[b]][s_i[a]] = c;
    }

    for(int i = 0; i < nVertex; i++)
        dis[i] = ew[0][i];
    dis[0] = 0;
    for(int i = 0; i < nVertex; i++){
        int u = -1, minn = INF;
        for(int j = 0; j < nVertex; j++){
            if(!isVis[j] && dis[j] < minn){
                minn = dis[j];
                u = j;
            }
        }
        isVis[u] = 1;
        for(int v = 0; v < nVertex; v++){
            if(u != v)
            {
                if(dis[v] > ew[u][v] + dis[u]){
                    dis[v] = ew[u][v] + dis[u];
                    prepath[v].clear();
                    prepath[v].push_back(u);
                }
                else if(dis[v] == ew[u][v] + dis[u]){
                    prepath[v].push_back(u);
                }
            }
        }
    }
    int rom = s_i["ROM"];
    dfs(rom);
    printf("%d %d %d %d\n", cntRo, dis[rom], ansHapy, (int)ansAvg);
    for(int i = anspath.size() - 1; i != 0; i--){
        cout << i_s[anspath[i]] << "->";
    }
    printf("ROM");
    return 0;
}

转载于:https://www.cnblogs.com/1pha/p/7899190.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, based on the given problem, here is a class diagram that represents the entities and their relationships: ``` +-----------------+ +-----------------+ | Country |<>----------| City | +-----------------+ +-----------------+ | | | | | - name | | - name | | - cities | | - roads | | | | | | + addCity() | | + addRoad() | | + removeCity() | | + removeRoad() | +-----------------+ +-----------------+ ^ | | | | | +----------------------+ | TravelingUnit | +----------------------+ | | | - type | | - driver | | - wheels/legs | | - weapons | | - canFire | | - maxSpeed | | | | + move() | | + captureByBandit() | +----------------------+ ^ | | | | | +---------------------+ | Driver | +---------------------+ | | | - nationality | | - name | | | +---------------------+ ``` Explanation: - The `Country` class represents a country and has a relationship with multiple `City` objects through the `cities` attribute. It also has attributes like `name` and methods like `addCity()` and `removeCity()` for managing cities. - The `City` class represents a city and has a relationship with multiple `Road` objects through the `roads` attribute. It has attributes like `name` and methods like `addRoad()` and `removeRoad()` for managing roads. - The `TravelingUnit` class represents a traveling unit and contains attributes like `type`, `driver`, `wheels/legs`, `weapons`, `canFire`, and `maxSpeed`. It has methods like `move()` for moving the unit and `captureByBandit()` for capturing the unit. - The `Driver` class represents a driver and contains attributes like `nationality` and `name`. It is associated with the `TravelingUnit` class. Note: This is a basic representation of the system based on the given problem statement. There might be additional classes or relationships required depending on the specific requirements of the system.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值