1003 Emergency (25 point(s))

1003 Emergency (25 point(s))

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Example:

#include<iostream>
#include<vector>
#include<list>

using namespace std;

typedef int Vertex;

struct Link {
    Vertex vertex;
    int dist;
};
struct Graph {
    int Nv;
    int Ne;
    vector<list<Link>> Adj;
    vector<Vertex> Team;
};
struct PathType {
    int dist;
    int team;
} Path;

int Count, Dist, Teams;

void DFS(Graph &G, Vertex v, Vertex c, vector<bool> &Visited)
{
    if(v == c) {
        Path.team += G.Team[c];
        if(Dist == -1 || Path.dist < Dist) {
            Dist = Path.dist;
            Count = 1;
            Teams = Path.team;
        } else if(Path.dist == Dist) {
            Count++;
            if(Teams < Path.team)
                Teams = Path.team;
        }
        Path.team -= G.Team[c];
    } else {
        if(Visited[v]) return ;
        Visited[v] = true;
        Path.team += G.Team[v];
        for(auto &x : G.Adj[v]) {
            if(!Visited[x.vertex]) {
                Path.dist += x.dist;
                DFS(G, x.vertex, c, Visited);
                Path.dist -= x.dist;
            }
        }
        Visited[v] = false;
        Path.team -= G.Team[v];
    }
}

void ShortestPath(Graph &G, Vertex v, Vertex c)
{
    vector<bool> Visited(G.Nv, false);
    Count = 0;
    Teams = 0;
    Dist = -1;
    Path.team = 0;
    Path.dist = 0;
    DFS(G, v, c, Visited);
    cout << Count << ' ' << Teams << endl;
}

int main()
{
    int N, M, C1, C2;
    cin >> N >> M >> C1 >> C2;
    Graph G;
    G.Nv = N;
    G.Ne = M;
    G.Adj.resize(N);
    G.Team.resize(N);
    for(int i = 0; i < N; i++){
        cin >> G.Team[i];
    }
    for(int i = 0; i < M; i++){
        int c1, c2, dist;
        cin >> c1 >> c2 >> dist;
        G.Adj[c1].push_back({c2, dist});
        G.Adj[c2].push_back({c1, dist});
    }
    ShortestPath(G, C1, C2);
}

思路:

查找图的最短路径,利用DFS,然后记录下最短路径下支持的最大救援队伍数量。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值