zoukankan      html  css  js  c++  java
  • PAT 甲级 1003 Emergency

    https://pintia.cn/problem-sets/994805342720868352/problems/994805523835109376

    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 (≤) - the number of cities (and the cities are numbered from 0 to N1), 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 Specification:

    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

    时间复杂度:$O(2 * N ^ 2)$

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    #define inf 0x3f3f3f3f
    int N, M, C1, C2;
    int cnt = 0;
    int mp[550][550];
    int team[550];
    int vis[550], dis[550];
    int see[550];
    int amount[550];
    
    void dijkstra(int act) {
        memset(vis, 0, sizeof(vis));
        memset(dis, inf, sizeof(dis));
    
        dis[act] = 0;
        int temp = act;
        see[act] = 1;
        amount[act] = team[act];
    
        for(int i = 0; i < N; i ++) {
            dis[i] = mp[act][i];
            if(dis[i] != inf && i != act) {
                amount[i] = amount[act] + team[i];
                see[i] = 1;
            }
        }
    
        for(int i = 0; i < N - 1; i ++) {
            int minn = inf;
            for(int j = 0; j < N; j ++) {
                if(dis[j] < minn && vis[j] == 0) {
                    minn = dis[j];
                    temp = j;
                }
            }
    
            vis[temp] = 1;
            for(int k = 0; k < N; k ++) {
                if(!vis[k] && dis[k] > dis[temp] + mp[temp][k]) {
                    dis[k] = dis[temp] + mp[temp][k];
                    amount[k] = amount[temp] + team[k];
                    see[k] = see[temp];
                }
                else if(!vis[k] && dis[k] == dis[temp] + mp[temp][k]) {
                    see[k] += see[temp];
                    if (amount[k] < amount[temp]+team[k])
                        amount[k] = amount[temp]+team[k];
                }
            }
        }
        return;
    }
    
    int main() {
        scanf("%d%d%d%d", &N, &M, &C1, &C2);
        for(int i = 0; i < N; i ++)
            scanf("%d", &team[i]);
    
        memset(mp, inf, sizeof(mp));
        for(int i = 1; i <= M; i ++) {
            int a, b, cost;
            scanf("%d%d%d", &a, &b, &cost);
            mp[a][b] = mp[b][a] = cost;
        }
    
        dijkstra(C1);
        printf("%d %d
    ", see[C2], amount[C2]);
        return 0;
    }
    

      

  • 相关阅读:
    eclipse中将web项目部署到tomcat
    mysql + Fluently NHibernate + WebAPI + Autofac
    无状态
    mysql备份及还原
    【jmeter】jmeter 常用组件 介绍
    【测试小方法】通过谷歌开发者工具手动修改位置信息
    【jmeter】jmeter 设置线程组 各个字段的概念
    【jmeter】jmete 安装介绍
    【腾讯位置服务web service】腾讯位置服务web service api
    【python】python 的 lambda 函数
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9668250.html
Copyright © 2011-2022 走看看