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

    题解:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <vector>
    #include <set>
    #include <map>
    #define LL long long
    #define INF 0x3f3f3f3f
    using namespace std;
    const int maxn=505;
    int n,m,c1,c2;
    int vis[maxn],num[maxn];
    int e[maxn][maxn];
    int dis[maxn];
    int w[maxn],weight[maxn];
    void init(){
        memset(vis,0,sizeof vis);
        memset(dis,INF,sizeof dis);
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i==j) e[i][j]=0;
                else e[i][j]=INF;
            }
        }
    }
    void Dijkstra(){
        for(int i=0;i<n;i++){
            dis[i]=e[c1][i];
        }
        dis[c1]=0;
        //vis[c1]=1;
        num[c1]=1;
        w[c1]=weight[c1];
        for(int i=0;i<n;i++){
            int minx=INF;
            int index=-1;
            for(int j=0;j<n;j++){
                if(!vis[j]&&dis[j]<minx){
                    minx=dis[j];
                    index=j;
                }
            }
            if(index==-1) return;
            vis[index]=1;
            for(int j=0;j<n;j++){
                if(!vis[j]&&e[index][j]<INF){
                    if(dis[index]+e[index][j]<dis[j]){
                        dis[j]=dis[index]+e[index][j];
                        num[j]=num[index];
                        w[j]=w[index]+weight[j];
                    }else if(dis[index]+e[index][j]==dis[j]){
                        num[j]=num[j]+num[index];
                        if(w[index]+weight[j]>w[j]){
                            w[j]=w[index]+weight[j];
                        }
                    }
                }
            }
        }
    
    }
    int main()
    {
        scanf("%d%d%d%d",&n,&m,&c1,&c2);
        init();
        for(int i=0;i<n;i++){
            scanf("%d",&weight[i]);
        }
        int a,b,c;
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&a,&b,&c);
            e[a][b]=c;
            e[b][a]=c;
        }
        Dijkstra();
        printf("%d %d
    ",num[c2],w[c2]);
        return 0;
    }
    


  • 相关阅读:
    【线程退出】linux线程退出的几个函数
    Apache Doris编译安装记录
    你所不知道的java编程思想
    thinking in java知识小记(一)
    一个程序员的修炼之路
    解决linux不能使用chmod更改权限的问题
    centos6.5配置无线网络
    ubuntu16.04 server安装小记
    vim的基本使用方法
    微信之父张小龙经典演讲164页PPT:《微信背后的产品观》
  • 原文地址:https://www.cnblogs.com/kzbin/p/9205210.html
Copyright © 2011-2022 走看看