Fire-Fighting Hero

赛后自己写了一个。。。因为舍不得开大数组,挂了三次。。。好在场上是队友做的, 1 A 1A 1A,hhh

题意:英雄从给定的 s s s点出发,求到所有点的最短路径最大值;消防员从 k k k个出发点随便选一个出发,求到所有点的最短路径最大值;并拿上面两个最大值进行比较,然后是“若,否则”的形式。

思路

  1. 显然对于英雄来说直接跑一个单源最短路即可
  2. 对于消防员而言,可以新建一个超级源点,跟其他出发点连上边权为 0 0 0的边,也同样跑一遍单源最短路即可
  3. 然后,没有然后了,就这么简单。(单源最短路推荐 d i j s t r a dijstra dijstra
  4. 数组记得开大呀!至少开到 m a x n m a x n + m a x n maxn*maxn+maxn maxnmaxn+maxn这么大

题面描述

#include "bits/stdc++.h"
#define hhh printf("hhh\n")
#define see(x) (cerr<<(#x)<<'='<<(x)<<endl)
using namespace std;
typedef long long ll;
typedef pair<int,int> pr;
inline int read() {int x=0;char c=getchar();while(c<'0'||c>'9')c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return x;}

const int maxn = 1e3+10;
const int mod = 1e9+7;
const double eps = 1e-7;

vector<int> st;
int N, M, S, K, C;
int dis[maxn], vis[maxn];
int head[maxn], to[maxn*maxn*2], nxt[maxn*maxn*2], cost[maxn*maxn*2], tot;

inline void max(int &a, int b) { if(a<b) a=b; }

inline void add_edge(int u, int v, int c) {
    ++tot, to[tot]=v, nxt[tot]=head[u], cost[tot]=c, head[u]=tot;
    ++tot, to[tot]=u, nxt[tot]=head[v], cost[tot]=c, head[v]=tot;
}

void dijstra(int s) {
    memset(dis,88,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[s]=0;
    priority_queue<pr,vector<pr>,greater<pr> > q;
    q.push(pr(0,s));
    while(q.size()) {
        int now=q.top().second; q.pop();
        if(vis[now]) continue; vis[now]=1;
        for(int i=head[now]; i; i=nxt[i]) {
            int v=to[i];
            if(dis[v]>dis[now]+cost[i]) {
                dis[v]=dis[now]+cost[i];
                q.push(pr(dis[v],v));
            }
        }
    }
}

int main() {
    //ios::sync_with_stdio(false); cin.tie(0);
    int T=read();
    while(T--) {
        st.clear(); memset(head,0,sizeof(head));
        N=read(), M=read(), S=read(), K=read(), C=read();
        for(int i=1; i<=K; ++i) st.push_back(read());
        for(int i=1; i<=M; ++i) {
            int u=read(), v=read(), c=read();
            add_edge(u,v,c);
        }
        int m1=0, m2=0;
        dijstra(S);
        for(int i=1; i<=N; ++i) max(m1,dis[i]);
        for(auto p: st) add_edge(0,p,0);
        dijstra(0);
        for(int i=1; i<=N; ++i) max(m2,dis[i]);
        if(m1<=m2*C) printf("%d\n", m1);
        else printf("%d\n", m2);
    }
}