1003 Emergency (25 分)

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int inf = 0x3fffffff;
const int maxn = 510;
int cost[maxn][maxn];
int d[maxn];
int npath[maxn];
int hands[maxn];
int used[maxn];
int team[maxn];

void dijkstra(int s, int end, int n){
    fill(d, d+n, inf);
    fill(used, used+n, 0);
    fill(hands, hands+n, 0);
    fill(npath, npath+n, 0);
    d[s] = 0; hands[s] = team[s]; npath[s] = 1;
    while(true){
        int v = -1;
        for(int u = 0; u < n; u++){
            if(!used[u] && (v == -1 || d[u] < d[v])) v = u;
        }
        if( v == -1) break;
        used[v] = 1;
        for(int u = 0; u < n; u++){
            if(d[u] > d[v] + cost[v][u]){
                d[u] = d[v] + cost[v][u];
                npath[u] = npath[v];
                hands[u] = hands[v] + team[u];
            }else if(d[u] == d[v] + cost[v][u]){
                npath[u] += npath[v];
                hands[u] = max(hands[u], hands[v]+team[u]);
            }
        }
    }
    printf("%d %d\n", npath[end], hands[end]);
}

int main(){
    int n, m, start, end;
    scanf("%d%d%d%d", &n, &m, &start, &end);
    for(int i = 0; i < n; i++)
        scanf("%d", &team[i]);
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++) cost[i][j] = inf;
    int c1, c2, x;
    for(int i = 0; i < m; i++){
        scanf("%d%d%d", &c1, &c2, &x);
        cost[c1][c2] = cost[c2][c1] = x;
    }
    dijkstra(start, end, n);
        return 0;
}

 

你可能感兴趣的:(PAT)