题目描述
This is an era of team success, but also an era of heroes. Throughout the ages, there have been numerous examples of using the few to defeat the many. There are V
(Numbers 1 to V) fire-fighting points in ACM city. These fire-fighting points have E
roads to communicate with each other. Among them, there is a fire-fighting hero in the S
fire-fighting point, and the fire-fighting team is distributed in K
fire-fighting points. If a fire-fighting point needs to be put out, the fire-fighting hero or the fire-fighting team must arrive as soon as possible, that is, to choose the shortest route to arrive.
Today, our fire-fighting heroes want to challenge the fire-fighting team. The challenge is to: The maximum value of the shortest path for a fire-fighting hero to go to others fire-fighting points is compared with the maximum value of the shortest path for a fire-fighting team to go to others fire-fighting points from any point in their fire-fighting points. Because firefighting heroes are different and run faster, the maximum value of the shortest path they get should be discounted first, that is, multiplied by a coefficient of 1/C
, and then compared. The smaller one wins. Who is the real firefighter in this situation?
Who is the real firefighter in this situation?
Input
The first line contains a positive integer T
(1≤ T ≤ 10), which indicates that there are T cases of test data.
The format of each case of test data is as follows:
V
(1 ≤ V ≤ 1000), E
(V-1 ≤ E ≤ V*V/2), S
(1 ≤ S ≤ V), K
(1≤ K ≤ V) and C
(1 ≤ C ≤ 10), the meanings are shown above.Output
Each case of test data outputs one line, which is a integer. That is, the maximum value of the shortest path of the winner (If the fire hero wins, the maximum value before the discount should be output). A draw is also a victory for fire-fighting hero.
样例输入
1
4 7 3 2 2
1 4
1 2 7
1 3 2
1 4 6
2 1 1
2 4 1
3 2 1
3 4 3
样例输出
2
分析
最短路 dijkstra
*无向图
题意:有 v 个消防点, 有 e 条边,消防英雄在点 s 处, 消防队分布在 k 个点,求消防英雄到每个消防点的最短路中的最大值 ans,以及消防队到每个消防点的最短路中的最大值 sum,比较 ans/C 和 sum 的大小(为避免取整的麻烦,可以改为比较 ans 和 sum*C 的大小),ans/C ≤ sum 都是消防英雄赢,此时输出 ans,否则输出 sum。
先调用 dijkstra 函数求出 s 到所有消防点的最短路,得到 ans;
求消防队到所有消防点的最短路时,因为有多个起点,所以可以另开一个新源点编号为0,并初始化该点到所有消防队所在点的距离为0,这样只要调用一次 dijkstra 函数就可以求出所有消防队到其他消防点的最短路了,得到 sum;
最后比较大小输出结果即可。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define INF 0x3f3f3f3f
#define inf 1ll<<60
#define zero 1e-7
typedef long long ll;
const int N=1e3+5;
const int maxn=5e3+5;
const ll mod=1e9+7;
int p[N];//消防队所在消防点
int mp[N][N];//节点之间的距离
int dis[N];//节点与源点之间的最短距离
bool vis[N];//标记节点是否已访问
int v;
void dijkstra(int x) {//起点
memset(vis, false, sizeof(vis));
vis[x]=true;
for(int i=1; i<=v; i++)
dis[i]=mp[x][i];
for(int i=1; i<v; i++) {
int mind=INF, temp=i;
for(int j=1; j<=v; j++) {
if(!vis[j] && dis[j]<mind) {
mind=dis[j];
temp=j;
}
}
vis[temp]=true;
for(int j=1; j<=v; j++)
if(!vis[j]) dis[j]=min(dis[temp]+mp[temp][j], dis[j]);
}
return ;
}
int main() {
int t, e, s, k, c;//点数,边数,消防英雄所在点,消防队所在点数,1/c
scanf("%d", &t);
while(t--) {
scanf("%d %d %d %d %d", &v, &e, &s, &k, &c);
for(int i=1; i<=k; i++)
scanf("%d", &p[i]);
for(int i=0; i<=v; i++) //记得从0开始
for(int j=0; j<=v; j++)
mp[i][j]=mp[j][i]=(i==j ? 0 : INF);
int a, b, l;
for(int i=1; i<=e; i++) {
scanf("%d %d %d", &a, &b, &l);
if(l<mp[a][b]) mp[a][b]=mp[b][a]=l;//处理重边
}
int ans=0;
dijkstra(s);
for(int i=1; i<=v; i++) {
if(dis[i]<INF ) ans=max(dis[i], ans);
}
//自定义一个源点
for(int i=1; i<=k; i++)
mp[0][p[i]]=mp[p[i]][0]=0;
dijkstra(0);
int sum=0;
for(int i=1; i<=v; i++) {
if(dis[i]<INF) sum=max(dis[i], sum);
}
if(ans<=sum*c) printf("%d\n", ans);
else printf("%d\n", sum);
}
return 0;
}