标签:
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.
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.
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
2 4
#include <bits/stdc++.h> #define LL long long #define Pr pair<int,int> #define VI vector<int> using namespace std; const int INF = 0x3f3f3f3f; const int mod = 1e9+7; const double eps = 1e-8; int mp[555][555]; int head[555]; int cnt[555],dis[2][555]; int val[555]; int st,en,n; void spfa() { queue <int> q; q.push(st); memset(cnt,0,sizeof(cnt)); memset(dis[0],INF,sizeof(int)*555); memset(dis[1],0,sizeof(int)*555); dis[0][st] = 0; dis[1][st] = val[st]; cnt[st] = 1; int u,v,w; while(!q.empty()) { u = q.front(); q.pop(); if(u == en) continue; for(v = 0; v < n; ++v) { w = mp[u][v]; if(dis[0][v] > dis[0][u]+w) { dis[0][v] = dis[0][u]+w; dis[1][v] = dis[1][u]+val[v]; if(!cnt[v]) q.push(v); cnt[v] = cnt[u]; } else if(dis[0][v] == dis[0][u]+w) { dis[1][v] = max(dis[1][u]+val[v],dis[1][v]); if(!cnt[v]) q.push(v); cnt[v] += cnt[u]; } // printf("%d->%d dis:%d val:%d cnt:%d\n",u,v,dis[0][v],dis[1][v],cnt[v]); } cnt[u] = 0; } } int main() { int m,u,v,w; scanf("%d%d%d%d",&n,&m,&st,&en); for(int i = 0; i < n; ++i) scanf("%d",&val[i]); memset(mp,INF,sizeof(mp)); while(m--) { scanf("%d%d%d",&u,&v,&w); if(mp[u][v] > w) mp[u][v] = mp[v][u] = w; } spfa(); printf("%d %d\n",cnt[en],dis[1][en]); return 0; }
标签:
原文地址:http://blog.csdn.net/challengerrumble/article/details/51339026