zoukankan      html  css  js  c++  java
  • PAT 1003 Emergency[图论]

    1003 Emergency (25)(25 分)

    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

    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, C1 and C2 - 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 c1, c2 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 C1 to C2.

    Output

    For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, 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
    //转自:https://www.cnblogs.com/549294286/p/3571448.html
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    
    using namespace std;
    
    #define INF 0x3f3f3f3f
    #define MX 501
    
    int mp[MX][MX];
    int v[MX];
    int dist[MX];
    int amount[MX];
    int teams[MX];
    int pathcount[MX];
    int N,M,start,en;
    
    void dijkstra(int s){
        amount[s] = teams[s];//s为开始时
        dist[s] = 0;
        pathcount[s] = 1;//目前有一条路
    
        while (1){
            int u, dmin=INF;
            for (int i=0; i<N; i++){
                if (v[i]==0 && dist[i]<dmin){//v一开始全为0
                    dmin = dist[i];//第一次循环,选出来的点是s
                    u = i;//
                }
            }
            if (dmin==INF || u==en) break;
            v[u] = 1;//设置为被访问过,每次都选最小的那个点出来。
            printf("\n");
            for (int i=0; i<N; i++){
                if(v[i]==0){
                    if (dist[i] > dist[u] + mp[u][i]){
                        dist[i] = dist[u] + mp[u][i];
                        amount[i] = amount[u] + teams[i];
                        pathcount[i] = pathcount[u];
                    }else if (mp[u][i]!=INF&&dist[i] == dist[u] + mp[u][i]){
                        pathcount[i] += pathcount[u];
                        if (amount[i] < amount[u] + teams[i])
                            amount[i] = amount[u] + teams[i];
                    }
                    printf("%d %d %d %d %d\n",u,i,dist[i],pathcount[i],amount[i]);
                }
    
            }
        }
    }
    
    int main()
    {
        scanf("%d%d%d%d", &N,&M,&start,&en);
        for (int i=0; i<N; i++)
        {
            scanf("%d", &teams[i]);//每个点所有的救援队数
        }
        for (int i=0; i<N; i++)
        {
            dist[i] = INF;//每一个距离初始化为正无穷。
            for (int j=0; j<N; j++)
                mp[i][j] = INF;
        }
        for (int i=0; i<M; i++)
        {
            int c1, c2, L;
            scanf("%d%d%d", &c1,&c2,&L);
            mp[c1][c2] = mp[c2][c1] = L;//将没有边的设置为正无穷。
        }
    
        dijkstra(start);
        printf("%d %d", pathcount[en], amount[en]);
    
        return 0;
    }

    //意思就是给出一个图,每个点有权值,边有权重,找出最短的权重的边,并且记录点的最大的权值和。就是要找出所有的最短边,并且进行比较。

    //这里是给出了一个循环,每次找出最短的,并且遍历所有的点。看是否可以更新距离。如果到当前点的距离相等的话,那么就路径数相加,并且amount[i]取两者较大值。这是一种题型。应该会的。

    代码来自:https://www.liuchuo.net/archives/2359

    #include <iostream>
    #include <algorithm>
    using namespace std;
    int n, m, c1, c2;
    int e[510][510], weight[510], dis[510], num[510], w[510];
    bool visit[510];
    const int inf = 99999999;
    int main() {
        scanf("%d%d%d%d", &n, &m, &c1, &c2);
        for(int i = 0; i < n; i++)
            scanf("%d", &weight[i]);
        fill(e[0], e[0] + 510 * 510, inf);//原来二维数组是这样初始化的。
        fill(dis, dis + 510, inf);
        int a, b, c;
        for(int i = 0; i < m; i++) {
            scanf("%d%d%d", &a, &b, &c);
            e[a][b] = e[b][a] = c;
        }
        dis[c1] = 0;
        w[c1] = weight[c1];
        num[c1] = 1;//到当前点的共计路径条数。
        for(int i = 0; i < n; i++) {
            int u = -1, minn = inf;
            for(int j = 0; j < n; j++) {
                if(visit[j] == false && dis[j] < minn) {
                    u = j;
                    minn = dis[j];
                }
            }
            if(u == -1) break;//迪杰斯特拉都有这么一个判断,就是找不到其他的可以访问的点了。
            visit[u] = true;
            for(int v = 0; v < n; v++) {
                if(visit[v] == false && e[u][v] != inf) {
                    if(dis[u] + e[u][v] < dis[v]) {
                        dis[v] = dis[u] + e[u][v];
                        num[v] = num[u];
                        w[v] = w[u] + weight[v];//点权相加
                    } else if(dis[u] + e[u][v] == dis[v]) {
                        num[v] = num[v] + num[u];//如果相等,就把两者相加。
                        if(w[u] + weight[v] > w[v])
                            w[v] = w[u] + weight[v];
                    }
                }
            }
        }
        printf("%d %d", num[c2], w[c2]);
        return 0;
    }

    //感觉这个代码更好理解一点,就是使用迪杰斯特拉的变形,也就是在更新边的同时加了几个判断条件,通常来说迪杰斯特拉是遍历一遍就会结束了,但是这个加了一个for循环,那么就每次都会选出一个最短的边(到c1,即原点),选出后才会被标记为已经访问过。

  • 相关阅读:
    【JOB】Oracle中JOB的创建方法以及一个细节的探究
    PHP安装加载yaf扩展
    微信开发(1) -- 将本地开发环境映射到公网访问
    修改Nginx与Apache上传文件大小限制
    liunx系统安装composer与配置
    Mysql 5.7 liunx 忘记密码的补救方法
    Vim使用技巧(4) -- 命令行模式 【持续更新】
    Vim使用技巧(3) -- 可视化模式技巧 【持续更新】
    Vim使用技巧(2) -- 插入模式技巧 【持续更新】
    Postman 安装及使用入门教程
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9334574.html
Copyright © 2011-2022 走看看