ACM-ICPC 2018 沈阳赛区网络预赛 Made In Heaven(K短路)题解

Posted kirinsb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ACM-ICPC 2018 沈阳赛区网络预赛 Made In Heaven(K短路)题解相关的知识,希望对你有一定的参考价值。

思路:K短路裸题

代码:

#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<stack>
#include<string>
#include<cmath>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = 1000 + 10;
const int maxm = 10000 + 10;
const int seed = 131;
const ll MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
struct Edge{
    int v, w, next;
};
struct As{
    int f, g, pos;
    bool operator < (const As a) const{
        return a.f == f? a.g < g : a.f < f;
    }
};
int n, m, k, tot1, tot2, T;
int head1[maxn], head2[maxn], dis[maxn];
bool vis[maxn];
Edge edge1[maxm], edge2[maxm];
void init(){
    memset(head1, -1, sizeof(head1));
    memset(head2, -1, sizeof(head2));
    tot1 = tot2 = 0;
}
void addEdge(int u, int v, int w){
    edge1[tot1].v = v;
    edge1[tot1].w = w;
    edge1[tot1].next = head1[u];
    head1[u] = tot1++;

    edge2[tot2].v = u;
    edge2[tot2].w = w;
    edge2[tot2].next = head2[v];
    head2[v] = tot2++;
}
void spfa(int st){
    for(int i = 0; i <= n; i++) dis[i] = INF;
    memset(vis, false, sizeof(vis));
    vis[st] = true;
    dis[st] = 0;
    queue<int> q;
    while(!q.empty()) q.pop();
    q.push(st);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int i = head2[u]; i != -1; i = edge2[i].next){
            int v = edge2[i].v;
            int w = edge2[i].w;
            if(dis[v] > dis[u] + w){
                dis[v] = dis[u] + w;
                if(!vis[v]){
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
}
int Astar(int st, int end){
   int cnt = 0;
   priority_queue<As> q;
   while(!q.empty()) q.pop();
   if(st == end) k++;
   if(dis[st] == INF) return -1;
   As a, b;
   a.pos = st, a.g = 0, a.f = a.g + dis[st];
   q.push(a);
   while(!q.empty()){
        a = q.top();
        q.pop();
        if(a.f > T) return -1;
        if(a.pos == end && a.f <= T){
            cnt++;
            if(cnt == k) return 1;
        }
        for(int i = head1[a.pos]; i != -1; i = edge1[i].next){
            b.pos = edge1[i].v;
            b.g = a.g + edge1[i].w;
            b.f = b.g + dis[b.pos];
            q.push(b);
        }
   }
   return -1;
}
int main(){
    int s, e;
    while(~scanf("%d%d", &n, &m)){
        init();
        scanf("%d%d%d%d", &s, &e, &k, &T);
        for(int i = 1; i <= m; i++){
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            addEdge(u, v, w);
        }
        spfa(e);
        int ans = Astar(s, e);
        if(ans == -1)
            printf("Whitesnake!
");
        else
            printf("yareyaredawa
");
    }
    return 0;
}

 

以上是关于ACM-ICPC 2018 沈阳赛区网络预赛 Made In Heaven(K短路)题解的主要内容,如果未能解决你的问题,请参考以下文章

ACM-ICPC 2018 沈阳赛区网络预赛

ACM-ICPC 2018 沈阳赛区网络预赛

ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph

ACM-ICPC 2018 沈阳赛区网络预赛 K. Supreme Number

ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph (贪心)

ACM-ICPC 2018 沈阳赛区网络预赛 KSupreme Number