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
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.
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 1Sample Output
2 4
///1003 dfs || dijkstra
#include<bits/stdc++.h>
const int maxn =10010;
const int INF =0x7FFFFF;
using namespace std;
int n,m,st,en,team[maxn]={0},mp[502][502],vis[maxn]={0},shortnum,shortdis,amountmax;
void dfs(int s,int dis,int teams){
if(s==en){
if(dis<shortdis){
shortnum=1;
amountmax=teams;
shortdis=dis;
}
else if(dis==shortdis){
shortnum++;
amountmax=max(amountmax,teams);
}
return ;
}
vis[s]=1;
for(int i=0;i<n;i++){
if(vis[i]==0&&mp[s][i]>0)dfs(i,dis+mp[s][i],teams+team[i]);
}
vis[s]=0;
}
int main()
{
scanf("%d%d%d%d",&n,&m,&st,&en);
shortdis=INF;shortnum=amountmax=0;
for(int i=0;i<n;i++)scanf("%d",&team[i]);
for(int i=0;i<502;i++)for(int j=0;j<502;j++)mp[i][j]=-1;
for(int i=0;i<m;i++){
int t1,t2,t3;
scanf("%d%d%d",&t1,&t2,&t3);
mp[t1][t2]=mp[t2][t1]=t3;
}
dfs(st,0,team[st]);
printf("%d %d\n",shortnum,amountmax);
return 0;
}
///djkstra
#include<bits/stdc++.h>
const int maxn =10010;
const int INF = 0x7FFFFF;
using namespace std;
int n,m,st,en,team[maxn],mp[510][510],vis[maxn],dis[maxn],pathcount[maxn],amount[maxn];
void djkstra(int s){
amount[s]=team[s];
pathcount[s]=1;
dis[s]=0;
while(1){
int mint=INF,u;
for(int i=0;i<n;i++){
if(vis[i]==0&&dis[i]<mint){
mint=dis[i];
u=i;
}
}
if(mint==INF||u==en)break;
vis[u]=1;
for(int v=0;v<n;v++){
if(vis[v]==0){
if(dis[v]==dis[u]+mp[u][v]){
pathcount[v]+=pathcount[u];
amount[v]=max(amount[v],amount[u]+team[v]);
}
else if(dis[v]>dis[u]+mp[u][v]){
dis[v]=dis[u]+mp[u][v];
pathcount[v]=pathcount[u];
amount[v]=amount[u]+team[v];
}
}
}
}
}
int main()
{
scanf("%d%d%d%d",&n,&m,&st,&en);
for(int i=0;i<n;i++)scanf("%d",&team[i]);
for(int i=0;i<n;i++){
dis[i]=INF;
for(int j=0;j<n;j++)mp[i][j]=INF;
}
for(int i=0;i<m;i++){
int t1,t2,t3;
scanf("%d%d%d",&t1,&t2,&t3);
mp[t1][t2]=mp[t2][t1]=t3;
}
djkstra(st);
printf("%d %d\n",pathcount[en],amount[en]);
return 0;
}
本文介绍了一种使用深度优先搜索(DFS)和迪杰斯特拉算法(Dijkstra)来解决应急救援中寻找从当前城市到目标城市的最短路径及沿途最多救援队伍的问题。
1312

被折叠的 条评论
为什么被折叠?



