计蒜客 Fire-Fighting Hero
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 VV (Numbers 11 to VV) fire-fighting points in ACM city. These fire-fighting points have EE roads to communicate with each other. Among them, there is a fire-fighting hero in the SS 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 \frac{1}{C} C1 , and then compared. The smaller one wins. Who is the real firefighter in this situation?
Who is the real firefighter in this situation?
Input
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.
Examples
样例输入复制
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
Hint
题意:
在一个无向带权图中(考虑重边), 比较s点到其他点的最大最短路径和k个点到其他点的最大最短路径的大小, 前者要除以系数C, 输出较小的
题解:
考虑对k个点进行缩点, 增加一个超级源点0, 双向连接k个点, 权值为0, 这样就可以快速求出k个点到其他点的最短路径
注意要先求出s点的最短路径再加点, 因为加点会影响s点的最短路径.
然后比较即可
经验小结:
#include<bits/stdc++.h>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef long long LL;
const int inf = 1 << 30;
const int maxn = 1010;
const int maxm = 1e6+10;
int N, M, head[maxn], ecnt, K, k[maxn];
struct Edge{
int u, v, w, next;
}es[maxm<<2];
void addEgde(int u, int v, int w){
for(int i = head[u]; i != -1; i = es[i].next)
if(es[i].v == v && w < es[i].w){ //判重边
es[i].w = w;
return;
}
es[ecnt].u = u, es[ecnt].v = v, es[ecnt].w = w;
es[ecnt].next = head[u], head[u] = ecnt++;
}
void init(){
ecnt = 0;
ms(head, -1);
ms(es, 0);
}
int d[maxn];
bool used[maxn];
typedef pair<int, int> P;
void Dijkstra(int s){
int u, v, w;
priority_queue<P, vector<P>, greater<P> > q;
ms(used, 0);
for(int i = 0; i <= N; ++i) d[i] = inf;
q.push(P(d[s]=0, s));
while(!q.empty()){
P cur = q.top();
q.pop();
u = cur.second;
if(used[u]) continue;
used[u] = true;
for(int i = head[u]; i != -1; i = es[i].next){
v = es[i].v, w = es[i].w;
if(d[u] + w < d[v]){
d[v] = d[u] + w;
q.push(P(d[v], v));
}
}
}
}
int main() {
int T, s, c, u, v, w;
cin >> T;
while(T--){
init();
cin >> N >> M >> s >> K >> c;
for(int i = 0; i < K; ++i)
cin >> k[i];
while(M--){
cin >> u >> v >> w;
addEgde(u, v, w);
addEgde(v, u, w);
}
int max0 = 0, maxs = 0;
Dijkstra(s);
for(int i = 1; i <= N; ++i)
maxs = max(maxs, d[i]);
for(int i = 1; i < K; ++i){
addEgde(k[0], k[i], 0);
addEgde(k[i], k[0], 0);
}
Dijkstra(k[0]);
for(int i = 1; i <= N; ++i)
max0 = max(max0, d[i]);
if(maxs <= max0*c)
cout << maxs << endl;
else
cout << max0 << endl;
}
return 0;
}