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, C 1 and C 2 - 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 c 1 , c 2 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 C 1 to C 2 .
Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C 1 and C 2 , 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
题解:救火队,最短距离最多人数。
Code1:(dijkstra)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<stack>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int n,m,s,d;
int map[505][505],dis[505],path[505],v[505];
int vis[505],way[505];
void Dijkstra()
{
memset(dis,INF,sizeof(dis));
dis[s]=0;
path[s]=v[s];
way[s]=1;
for(int i=1; i<n; i++)
{
int min=INF,t=-1;
for(int j=0; j<n; j++)
if(!vis[j]&&dis[j]<min)
{
min=dis[j];
t=j;
}
vis[t]=1;
for(int j=0; j<n; j++)
{
if(vis[j])
continue;
if(dis[t]+map[t][j]<dis[j])
{
dis[j]=dis[t]+map[t][j];
path[j]=v[j]+path[t];
way[j]=way[t];
}
else if(dis[t]+map[t][j]==dis[j])
{
if(path[j]<path[t]+v[j])
{
path[j]=path[t]+v[j];
}
way[j]+=way[t];
}
}
}
}
int main()
{
int c1,c2,L;
memset(map,INF,sizeof(map));
scanf("%d %d %d %d",&n,&m,&s,&d);
for(int i=0; i<n; i++)
scanf("%d",&v[i]);
for(int i=0; i<m; i++)
{
scanf("%d %d %d",&c1,&c2,&L);
map[c1][c2]=map[c2][c1]=L;
}
Dijkstra();
printf("%d %d\n",way[d],path[d]);
return 0;
}
Code2(dfs):
#include <stdio.h>
#include <math.h>
#include<string.h>
#define INF 0x3f3f3f3f
int u[505]= {0};
int teams[505]= {0};
int dist[505];
int mp[505][505];
int n,m,st,en;
int shortNum=0,maxteam=0,mindist=INF;
void dfs(int s,int dis,int team)
{
if(s==en)
{
if(dis<mindist)
{
mindist=dis;
shortNum=1;
maxteam=team;
}
else if(dis==mindist)
{
shortNum++;
if(team>maxteam)
maxteam=team;
}
return ;
}
u[s]=1;
for(int i=0; i<n; i++)
{
if(u[i]==0&&mp[s][i]>0)
{
dfs(i,dis+mp[s][i],team+teams[i]);
}
}
u[s]=0;
}
int main()
{
scanf("%d %d %d %d",&n,&m,&st,&en);
for(int i=0; i<n; i++)
scanf("%d",&teams[i]);
memset(mp,-1,sizeof(mp));
for(int i=0; i<m; i++)
{
int t1,t2,dis;
scanf("%d %d %d",&t1,&t2,&dis);
mp[t1][t2]=mp[t2][t1]=dis;
}
dfs(st,0,teams[st]);
printf("%d %d\n",shortNum,maxteam);
return 0;
}