1003 Emergency (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 Specification:
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 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
题意:给你一个地图,急救员在其中一个城市,城市之间有距离,每个城市有些许急救员,问:在给出的两点,从c1到c2最短路有多少条,然后可以聚集的最多急救员可以有多少个?
解析:dijkstra最短路+松弛
开两个数组,tot[]表示最短路数,num[]表示可以聚集的最多急救员可以有多少个
#include<bits/stdc++.h>
using namespace std;
#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}
const int maxn=550;
int n,m,c1,c2;
int ma[maxn][maxn],a[maxn],dis[maxn],vis[maxn];
int num[maxn];//从起点到i的最大权重
int tot[maxn];//记录最短路径条数
void init()
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(i==j)ma[i][j]=0;
else ma[i][j]=inf;
}
}
mem(num,0);
mem(dis,inf);
mem(vis,0);
}
void dijkstra()
{
dis[c1]=0;
tot[c1]=1;
num[c1]=a[c1];
for(int i=0; i<n; i++)
{
int minn=inf,pos;
for(int j=0; j<n; j++)
{
if(!vis[j]&&dis[j]<minn)
{
pos=j;
minn=dis[j];
}
}
vis[pos]=1;
for(int j=0; j<n; j++)
{
if(dis[pos]+ma[pos][j]==dis[j]&&!vis[j])
{
//松弛
tot[j]+=tot[pos];
num[j]=max(num[j],num[pos]+a[j]);
}
if(dis[pos]+ma[pos][j]<dis[j]&&!vis[j])
{
dis[j]=dis[pos]+ma[pos][j];
tot[j]=tot[pos];
num[j]=num[pos]+a[j];
}
}
}
}
int main()
{
scanf("%d%d%d%d",&n,&m,&c1,&c2);
init();
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(int i=0; i<m; i++)
{
int v,w,c;scanf("%d%d%d",&v,&w,&c);
ma[v][w]=c;
ma[w][v]=c;
}
dijkstra();
printf("%d %d\n",tot[c2],num[c2]);
return 0;
}