码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-09-12 01:11:14      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:priority   span   mod   als   mem   color   eof   init   print   

思路: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!\n");
        else
            printf("yareyaredawa\n");
    }
    return 0;
}

 

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

标签:priority   span   mod   als   mem   color   eof   init   print   

原文地址:https://www.cnblogs.com/KirinSB/p/9631767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!