ACM-ICPC 2018 沈阳赛区网络预赛D-Made In Heave(A*+SPFA求第K最短路)

ACM-ICPC 2018 沈阳赛区网络预赛(D-Made In Heaven)A*+SPFA求第K最短路

One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. However, Pucci the father somehow knows it and wants to stop her. There are NNN spots in the jail and MMM roads connecting some of the spots. JOJO finds that Pucci knows the route of the former (K−1)(K-1)(K−1)-th shortest path. If Pucci spots JOJO in one of these K−1K-1K−1 routes, Pucci will use his stand Whitesnake and put the disk into JOJO’s body, which means JOJO won’t be able to make it to the destination. So, JOJO needs to take the KKK-th quickest path to get to the destination. What’s more, JOJO only has TTT units of time, so she needs to hurry.

JOJO starts from spot SSS, and the destination is numbered EEE. It is possible that JOJO’s path contains any spot more than one time. Please tell JOJO whether she can make arrive at the destination using no more than TTT units of time.
Input

There are at most 505050 test cases.

The first line contains two integers NNN and MMM (1≤N≤1000,0≤M≤10000)(1 \leq N \leq 1000, 0 \leq M \leq 10000)(1≤N≤1000,0≤M≤10000). Stations are numbered from 111 to NNN.

The second line contains four numbers S,E,KS, E, KS,E,K and TTT ( 1≤S,E≤N1 \leq S,E \leq N1≤S,E≤N, S≠ES \neq ES≠E, 1≤K≤100001 \leq K \leq 100001≤K≤10000, 1≤T≤1000000001 \leq T \leq 1000000001≤T≤100000000 ).

Then MMM lines follows, each line containing three numbers U,VU, VU,V and WWW (1≤U,V≤N,1≤W≤1000)(1 \leq U,V \leq N, 1 \leq W \leq 1000)(1≤U,V≤N,1≤W≤1000) . It shows that there is a directed road from UUU-th spot to VVV-th spot with time WWW.

It is guaranteed that for any two spots there will be only one directed road from spot AAA to spot BBB (1≤A,B≤N,A≠B)(1 \leq A,B \leq N, A \neq B)(1≤A,B≤N,A≠B), but it is possible that both directed road

题意:

求第k短路

分析:

使用A*+SPFA模板

关于其原理可以看一下这篇博客,但是里面的代码不要看,是错误,可以看下面AC的代码

关于A*算法的讲解可以看这篇

扫描二维码关注公众号,回复: 3224757 查看本文章

ACcode:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 11000;
const int MAXM = 110000;
const int INF = 0xffffff0;

struct EdgeNode{
    int to;
    int w;
    int Next;
}Edge[MAXM],Edge1[MAXM];

int head[MAXN],head1[MAXN];

struct Node{
    int to;
    int g,f;//f() = g() + h();g()是实际走的距离即当前点离起点的距离,h()为启发函数,为当前点到终点的最短距离(通过spfa预处理了)
            //f()便是估值函数,每次优先走f()值最小的,到达终点k次便是k短路
    bool operator < (const Node &r)const{
        if(r.f == f)
            return g > r.g;
        return f > r.f;
    }
};

int vis[MAXN],dis[MAXN];

void SPFA(int s,int N){
    for(int i = 0; i <= N; i++){
        dis[i] = INF;
    }
    memset(vis,0,sizeof(vis));
    vis[s] = 1;
    dis[s] = 0;
    queue<int> q;
    q.push(s);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for(int i = head1[u]; i != -1; i = Edge1[i].Next){
            int tmp = dis[u] + Edge1[i].w;
            int v = Edge1[i].to;
            if(dis[v] > tmp){
                dis[v] = tmp;
                if(!vis[v]){
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
}

int A_star(int st,int ed,int N,int k){
    Node e,ne;
    int cnt = 0;
    priority_queue<Node> q;
    if(dis[st] == INF) return -1;
    e.to = st;
    e.g = 0;
    e.f = e.g + dis[e.to];
    q.push(e);
    while(!q.empty()){
        e = q.top();
        q.pop();
        if(e.to == ed)
            cnt++;
        if(cnt == k)
            return e.g;
        int u = e.to;
        for(int i = head[u]; i != -1; i = Edge[i].Next){
            ne.to = Edge[i].to;
            ne.g = e.g + Edge[i].w;
            ne.f = ne.g + dis[ne.to];
            q.push(ne);
        }
    }
    return -1;
}
int main(){
    int n,m,u,v,w,s,t,k,T;
    while(~scanf("%d%d",&n,&m)){
        memset(Edge,0,sizeof(Edge));
        memset(Edge1,0,sizeof(Edge1));
        memset(head,-1,sizeof(head));
        memset(head1,-1,sizeof(head1));
        scanf("%d%d%d%d",&s,&t,&k,&T);
        for(int i = 0; i < m; i++){
            scanf("%d%d%d",&u,&v,&w);
            Edge[i].to = v;
            Edge[i].w = w;
            Edge[i].Next = head[u];
            head[u] = i;

            Edge1[i].to = u;//反向建图
            Edge1[i].w = w;
            Edge1[i].Next = head1[v];
            head1[v] = i;
        }
        SPFA(t,n);//用spfa出终点到任意一点的最短距离(或任意一点到终点的距离)
        int ans = A_star(s,t,n,k);
        if(ans > T || ans == -1)
            printf("Whitesnake!\n");
        else
            printf("yareyaredawa\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/82586711