zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    `cd -`
    .git文件夹过大的解决方法
    避免对函数参数的修改
    2020年10月 修改Ant Design Pro底部版权信息的方法
    度目-人脸应用套件 文档中的坑
    "A little like that j-thing"
    Linux安装MySQL后设置密码
    append对len和cap的影响
    s := []int{0, 1, 2, 3, 8: 100}
    Python学习笔记
  • 原文地址:https://www.cnblogs.com/KirinSB/p/9631767.html
Copyright © 2011-2022 走看看