1003. Emergency (25)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
题目大意:
作为一个城市的紧急救援队长,你会得到一张你国家的特殊地图。这张图显示了由一些道路连成
的分散城市。在这张图上标注着每个城市救援队的数量和在每两个城市之间每条道路的长度。当你从其他城市接到紧急电话时,你的任务是把救援队员尽可能快的带到事发地,与此同时,尽可能多的给他们打电话。
输入:
每个输入文件包括一个测试用例。对于每个测试用例,第一行包含4个正整数:N(<=500)-城市的数量(城市的编号是从0到N-1),M-道路的数量,C1和C2-分别是你现在所在的城市和你必须拯救的城市。下一行包含N个整数,第i个整数是第i个城市的救援队的数目。然后接下来是M行,每一行用c1,c2,和L三个整数描述,这三个数分别表示由一条道路连接的两个城市和和那条路的长度。他保证从C1到C2至少存在一条道路。
输出:
对于每个测试用例,在一行中打印两个数字:C1和C2之间的不同最短路径的数量,以及你能聚集的最大的救援队的数量。
直线上的所有数字都必须被一个空格隔开,在一行的末尾没有多余的空格。
方法一:Dijkstra
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MAX 501
int map[MAX][MAX],visited[MAX],path_number[MAX],dist[MAX],teams[MAX],team_account[MAX];
int c1,c2,n,m;
void Dijkstra()
{
int i,j,dmin,u;
path_number[c1]=1;
team_account[c1]=teams[c1];
dist[c1]=0;
for(i=0;i<n;i++)
{
dmin=INF;
for(j=0;j<n;j++)
{
if(dist[j]<dmin&&visited[j]==0)
{
dmin=dist[j];
u=j;
}
}
if(dmin==INF||u==c2)
{
break;
}
visited[u]=1;
for(j=0;j<n;j++)
{
if(visited[j]==0){
if(dist[j]>dist[u]+map[u][j])
{
dist[j]=dist[u]+map[u][j];
path_number[j]=path_number[u];
team_account[j]=team_account[u]+teams[j];
}
else if(dist[j]==dist[u]+map[u][j])
{
path_number[j]+=path_number[u];
if(team_account[j]<team_account[u]+teams[j])
{
team_account[j]=team_account[u]+teams[j];
}
}
}
}
}
}
int main()
{
int i,j,k,t,p;
scanf("%d %d %d %d",&n,&m,&c1,&c2);
for(i=0;i<n;i++)
{
scanf("%d",&teams[i]);
}
for(i=0;i<n;i++)
{
dist[i]=INF;
for(j=0;j<n;j++)
{
map[i][j]=INF;
}
}
for(i=0;i<m;i++)
{
scanf("%d %d %d",&k,&t,&p);
map[k][t]=map[t][k]=p;
}
Dijkstra();
printf("%d %d",path_number[c2],team_account[c2]);
return 0;
}
方法二:DFS
#include<bits/stdc++.h>
#define MAX 501
int map[MAX][MAX],visited[MAX],teams[MAX];
int pathaccount,maxaccount,mind,n,m;
void DFS(int start,int end,int pathlength,int teamaccount)
{
int i,j;
if(start==end)
{
if(pathlength<mind)
{
pathaccount=1;
mind=pathlength;
maxaccount=teamaccount;
}else if(pathlength==mind)
{
pathaccount++;
if(maxaccount<teamaccount)
{
maxaccount=teamaccount;
}
}
else
{
return ;
}
}
if(pathlength>mind)
{
return ;
}
for(i=0;i<n;i++)
{
if(visited[i]==0&&map[start][i]<INT_MAX)
{
visited[i]=1;
DFS(i,end,pathlength+map[start][i],teamaccount+teams[i]);
visited[i]=0;
}
}
}
int main()
{
int i,j,k,t,c1,c2,l;
mind=INT_MAX;
scanf("%d %d %d %d",&n,&m,&c1,&c2);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
map[i][j]=INT_MAX;
}
}
for(i=0;i<n;i++)
{
scanf("%d",&teams[i]);
}
for(i=0;i<m;i++)
{
scanf("%d %d %d",&k,&t,&l);
map[k][t]=map[t][k]=l;
}
DFS(c1,c2,0,teams[c1]);
printf("%d %d",pathaccount,maxaccount);
}
还没有评论,来说两句吧...