- 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.
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
#include<iostream>
using namespace std;
const int inf =99999999;
int path[510][510],num[510],dis[510],sumpath[510],sumnum[510],mini,u;
bool visited[510];//path记录两座城市之间的距离 dis表示待更新的到每个城市的距离
int main() //visited表示以及访问过的城市 num表示每个城市的救援队数量
{ //sumpath表示从出发点到i结点最短路径的条数 sumnum表示从出发点到i点救援队的数目之和
int n,m,c1,c2,a,b,c;
scanf("%d%d%d%d",&n,&m,&c1,&c2);
for(int i=0;i<n;i++)
cin>>num[i];
fill(dis,dis+510,inf);
fill(path[0],path[0]+510*510,inf); //先将未知的距离全部置无穷值,表示不可访问
for(int i=0;i<m;i++)
{
cin>>a>>b>>c;
path[a][b]=c;
path[b][a]=c;
}
dis[c1]=0; //决定从这里开始
sumpath[c1]=1;
sumnum[c1]=num[c1]; //将出发城市的救援人员数量以及道路数量初始化
for(int i=0;i<n;i++)
{
mini=inf;
u=-1;
for(int j=0;j<n;j++)
{
if(visited[j]==false&&dis[j]<mini)
{
mini=dis[j];
u=j;
}
} //找出dis最短的路径
if(u==-1) break; //如果找不到任何城市,说明所有城市都已经完成完全的访问
visited[u]=true;
for(int k=0;k<n;k++)
{
if(visited[k] == false && path[u][k] != inf) //如果这个城市未作为u访问过并且两座城市有路
{
if((dis[u]+path[u][k])<dis[k]) //如果u节点到它的邻居城市的距离小于它邻居城市的dis
{
dis[k]=dis[u]+path[u][k];
sumpath[k]=sumpath[u];
sumnum[k]=sumnum[u]+num[k];
}
else if((dis[u]+path[u][k])==dis[k]) //相等的情况
{
sumpath[k]=sumpath[k]+sumpath[u]; //注意这里更新的式子 不要写成sumpath[k]++;
if(sumnum[u]+num[k]>sumnum[k])
sumnum[k]=sumnum[u]+num[k];
}
}
}
}
cout<<sumpath[c2]<<" "<<sumnum[c2];
return 0;
}
注释尽可能写得详细了orz
多做做这种题应该就习惯了吧orz,会了以后应该算是送分题
这种是拿c实现的,还看到一种拿python实现的,简单粗暴,就是用了递归,耗时比较厉害,题目时间数据量再严苛一点,可能就过不了了。
inf = float('inf')
cities_num, roads_num, city_start, city_end = map(int,input().split())
rescue_teams = list(map(int,input().split()))
roads = [[inf for i in range(cities_num)] for i in range(cities_num)]
visit = [0 for i in range(cities_num)] #访问节点列表
min_roads = inf #最短路径
min_roads_count = 0 #最短路径条数
max_resue_sum =0 #最大救援队个数
for i in range(roads_num):
city1, city2, road = map(int,input().split())
roads[city1][city2] = road
roads[city2][city1] = road
def dfs(start, end, road, resue):
global min_roads, min_roads_count, max_resue_sum,roads_num,rescue_teams,visit
if(start == end):
if(road < min_roads):
min_roads_count = 1
min_roads = road
max_resue_sum = resue
elif(road == min_roads):
min_roads_count += 1
if(max_resue_sum < resue):
max_resue_sum = resue
return 0
if(road > min_roads):
return 1
for i in range(cities_num):
if(visit[i] == 0 and roads[start][i] != inf):
visit[i] = 1
dfs(i, end, road+roads[start][i], resue+rescue_teams[i])
visit[i] = 0
visit[city_start] = 1
dfs(city_start,city_end,0,rescue_teams[city_start])
print(min_roads_count,max_resue_sum)
此方法搬运自D_ry
这篇博客介绍了一个城市紧急救援团队领导在特殊地图上规划最快救援路线的问题。给定城市数量、道路、起始和目标城市,以及每个城市的救援队伍数量和道路长度,目标是找到最短路径并最大化途中集结的救援队伍。输入输出格式清晰,C语言和Python的解决方案被提及,其中Python的递归方法效率较低。

4374

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



