判断第k短路的权值是否小于T
直接把队友的代码拿过来了,一定很经典
#include <iostream> #include <queue> #include <cstring> #include <vector> using namespace std; const int maxn = 3050; const int INF = 0x7fffffff; int N, M, S, E, K, T; int inq[maxn]; int dis[maxn]; int cnt[maxn]; struct Edge { int u, v, w; Edge(int a, int b, int c):u(a), v(b), w(c){} }; struct Node { int d, v; Node(int a, int b):d(a), v(b){} bool operator < (const Node &x) const {return d+dis[v]>x.d+dis[x.v];} }; struct Node1 { int d, v; Node1(int a, int b):d(a), v(b){} bool operator < (const Node1 &x) const {return d>x.d;} }; vector<Edge> edge1; vector<Edge> edge2; vector<int> G1[maxn]; vector<int> G2[maxn]; inline void addedge(int a, int b, int c) { edge1.push_back(Edge(a,b,c)); G1[a].push_back(edge1.size()-1); edge2.push_back(Edge(b,a,c)); G2[b].push_back(edge2.size()-1); } void dijkstra_init() { priority_queue<Node1>Q; memset(inq, 0, sizeof(inq)); std::fill(dis, dis+maxn, INF); dis[E] = 0; Q.push(Node1(0, E)); while(!Q.empty()) { auto x = Q.top(); Q.pop(); if(inq[x.v]) continue; inq[x.v] = true; for(int i = 0; i < G2[x.v].size(); ++i) { Edge &m = edge2[G2[x.v][i]]; if(dis[m.v]>dis[m.u]+m.w) { dis[m.v] = dis[m.u]+m.w; Q.push(Node1(dis[m.v], m.v)); } } } } int k_th() { memset(cnt, 0, sizeof(cnt)); priority_queue<Node>Q; if(dis[S]>=INF) return -1; Node e(0, S); Q.push(e); while(!Q.empty()) { Node x = Q.top();Q.pop(); cnt[x.v]++; if(x.v == E) { if(x.d>T) return -1; if(cnt[x.v] == K) return x.d; } if (cnt[x.v] > K) continue; for(unsigned int i = 0; i < G1[x.v].size(); i++) { Node n(x.d+edge1[G1[x.v][i]].w, edge1[G1[x.v][i]].v); if(cnt[n.v] != K) Q.push(n); } } return -1; } int main() { while(scanf("%d%d", &N, &M) != EOF) { edge1.clear(); edge2.clear(); for(int i = 0; i < maxn; ++i) { G1[i].clear(); G2[i].clear(); } scanf("%d%d%d%d", &S, &E, &K, &T); for(int i = 1; i <= M; ++i) { int a, b, c; scanf("%d%d%d", &a, &b, &c); addedge(a, b, c); } dijkstra_init(); int ans = k_th(); if(ans <= T && ans != -1) printf("yareyaredawa "); else printf("Whitesnake! "); } return 0; }