1003 Emergency (25分)

34 篇文章 0 订阅

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.

Input Specification:
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C
​1
​​ and C
​2
​​ - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c
​1
​​ , c
​2
​​ and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C
​1
​​ to C
​2
​​ .

Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C
​1
​​ and C
​2
​​ , and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input:
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Sample Output:
2 4

这里我用了邻接表来实现Dijkstra(最好用邻接矩阵,因为1000点以内都是邻接矩阵比较方便),其实最短路径基本dijkstra就行了
邻接表的Node的结构体最好是定义一下Node的构造函数,这样push_back的时候比较方便

Node(int _v, int _dis):v(_v),dis(_dis){}//要注意后面没有分号

邻接表的图不用初始化,邻接矩阵的图要在main里面初始化
这题和基础的dij不同的是,它要你求最短路径的条数和最短路径上最大的点权和
所以我们重新定义了两个数组,一个是w数组,用来存源点到某点i最大的点权和,一个是num数组,用来存源点到某点i的最短路径条数
在拿出一个点优化路径时,多了一个选项,本来只有小于原本d[v]然后优化,并使num[v] = num[u],因为到u的路径就是num[u]条,u到v的路径只有1条,所以相等;使w[v] += weight[v]
在相等的情况下,不用优化d[v],如果w[v] < w[u] + weight[v],那么优化一下w[v]。然后不管点权和大于还是小于,路径条数一定要加上,num[v] += num[u]即可,因为num[v]现在还没有被源点访问过,所以加上num[u]会给他一个初始值。
最后输出num[ed]和w[ed]即可

#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
//cstring是memset的,algorithm是fill的
using namespace std;
const int MAXV = 510;
const int INF = 1000000000;

struct Node{
    int v, dis;
    Node(int _v, int _dis):v(_v),dis(_dis){}
};
vector<Node> Adj[MAXV];
int n, m, st, ed, d[MAXV], weight[MAXV];//d为从原点到i点的最短距离,weight为点权
int w[MAXV], num[MAXV];//w为从源点到i最大点权之和,num为从源点到i的最短路径条数
bool vis[MAXV] = {false};

void Dijkstra(int s){
    fill(d, d + MAXV, INF);
    memset(num, 0, sizeof(num));
    memset(w, 0, sizeof(w));
    d[s] = 0;
    w[s] = weight[s];
    num[s] = 1;
    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 j = 0; j < Adj[u].size(); j++){
            int v = Adj[u][j].v;
            int dis = Adj[u][j].dis;
            if(vis[v] == false){
                if(dis + d[u] < d[v]){
                    d[v] = dis + d[u];
                    num[v] = num[u];
                    w[v] = w[u] + weight[v];
                }
                else if(dis + d[u] == d[v]){
                    if(w[u] + weight[v] > w[v]){
                        w[v] = w[u] + weight[v];
                    }
                    num[v] += num[u];
                }
            }
        }
        
    }
}

int main(){
    cin >> n >> m >> st >>ed; 
    for(int i = 0; i < n; i++){
        cin >> weight[i];
    }
    int a, b, dis;
    for(int i = 0; i < m; i++){
        cin >> a >> b >> dis;
        Adj[a].push_back(Node(b,dis));
        Adj[b].push_back(Node(a,dis));
    }
    Dijkstra(st);
    cout << num[ed] << " "<< w[ed];
    cout << endl;
    return 0;
}

一眼就看出dij加dfs了吧,有条件的单元最短路径问题

//dij+dfs
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int INF = 1000000000;
const int maxn = 505;
int G[maxn][maxn];
bool vis[maxn] = {false};
int weight[maxn] = {0};
int dis[maxn];
vector<int> preNode[maxn];//记录start到达该顶点,这条路上,该顶点的前驱结点们

vector<int> tempPath;
vector<int> path;
int roadCnt = 0, maxRescue = 0;

int n,m,start,end1;

void dfs(int v){
    tempPath.push_back(v);
    if(v == start){
        int tempSum = 0;
        for(int i = 0; i < tempPath.size(); i++){
            tempSum += weight[tempPath[i]];
        }
        if(tempSum > maxRescue){
            maxRescue = tempSum;
        }
        roadCnt++;
        tempPath.pop_back();
        return;
    }
    for(int i = 0; i < preNode[v].size(); i++){
        dfs(preNode[v][i]);
    }
    tempPath.pop_back();
}

void dijkstra(int s){
    fill(dis,dis+maxn,INF);
    dis[s] = 0;
    for(int i = 0; i < n; i++){//循环n次,每次找出一个离原点最近的点(不需要和原点直接连通)
        int u = -1, minDis = INF;
        for(int j = 0; j < n; j++){//找出未访问的点里面离原点最近的点
            if(vis[j] == false && dis[j] < minDis){
                minDis = dis[j];
                u = j;
            }
        }
        if(u == -1) return;//证明和原点相邻的点已经全部处理过了
        vis[u] = true;
        for(int v = 0; v < n; v++){
            if(G[u][v] != INF && vis[v] == false){//两点之间相邻,对其进行收缩
                if(G[u][v] + dis[u] < dis[v]){
                    dis[v] = G[u][v]+dis[u];
                    preNode[v].clear();
                    preNode[v].push_back(u);
                }
                else if(G[u][v] + dis[u] == dis[v]){
                    preNode[v].push_back(u);
                }
            }
        }
    }
}

int main(){
    fill(G[0],G[0] + maxn *maxn,INF);
    cin>>n>>m>>start>>end1;
    for(int i = 0; i < n; i++){
        cin>>weight[i];
    }
    int u,v,dist;
    for(int i = 0; i < m; i++){
        cin>>u>>v>>dist;
        G[u][v] = G[v][u] = dist;
    }
    dijkstra(start);//此时已经记录下原点到每个点的最短路径们了
    dfs(end1);
    cout<<roadCnt<<" "<<maxRescue;
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值