ACM-ICPC 2018 沈阳赛区网络预赛-D:Made In Heaven(K短路+A*模板)

Made In Heaven
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 N spots in the jail and MM roads connecting some of the spots. JOJO finds that Pucci knows the route of the former (K−1)-th shortest path. If Pucci spots JOJO in one of these K−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 K-th quickest path to get to the destination. What’s more, JOJO only has T units of time, so she needs to hurry.

JOJO starts from spot S, and the destination is numbered E. 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 T units of time.

Input
There are at most 50 test cases.

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

The second line contains four numbers S,E,K and T ( 1≤S,E≤N, S≠E, 1≤K≤10000, 1≤T≤100000000 ).

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

It is guaranteed that for any two spots there will be only one directed road from spot AA to spot BB (1≤A,B≤N,A≠B), but it is possible that both directed road <A,B><A,B> and directed road <B,A><B,A>exist.

All the test cases are generated randomly.

Output
One line containing a sentence. If it is possible for JOJO to arrive at the destination in time, output “yareyaredawa” (without quote), else output “Whitesnake!” (without quote).
input

2 2
1 2 2 14
1 2 5
2 1 4

output
yareyaredawa
题意:
N个点,M条边,起始点为s,结束为n,求s到n的第k短的路的长度,判断长度是否大于T,如果大于,输出“Whitesnake!”,否则输出“yareyaredawa”
分析:
模板题目,这个题和POJ2449基本上一样,就多了一个和T的比较,还需注意的是要用long long ,否则会T掉的o(╥﹏╥)o
AC代码

#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <set>
#include <stack>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const int MOD=1e9+7;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define Abs(x) ((x)>=0?(x):-(x))
int n,m,s,t,K,T;
ll head[1010],rhead[1010],dis[1010],vis[1010];
int cnt;
struct node{//前向星
    ll to,next,w;
}e[10010],re[10010];//建立正向边和反向边
struct A{// f(x)=g(x)+h(x),k短路中g(x)表示已经走了多远,h(x)表示从这个点到终点的最短路
    ll f,g,from;//f函数,g函数
    bool operator <(const A &a) const{
        if(a.f==f) return a.g<g;
        return a.f<f;
    }
};
void init(){
    cnt=0;
    for(int i=0;i<=n;i++) head[i]=-1,rhead[i]=-1;
}
void add(int from,int to,int w){
    //建立正向边
    e[cnt].next=head[from];
    e[cnt].to=to;
    e[cnt].w=w;
    head[from]=cnt;
    //建立反向边,用于估计函数
    re[cnt].next=rhead[to];
    re[cnt].to=from;
    re[cnt].w=w;
    rhead[to]=cnt++;
}
void spfa(){

    queue<ll>q;
    rep(i,1,n) vis[i]=0,dis[i]=INF;
    q.push(t);
    vis[t]=1;
    dis[t]=0;
    while(!q.empty()){
        ll u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=rhead[u];i!=-1;i=re[i].next){
            ll to=re[i].to,w=re[i].w;
            if(dis[to]>dis[u]+w){
                dis[to]=dis[u]+w;
                if(!vis[to]){
                    vis[to]=1;
                    q.push(to);
                }
            }
        }
    }
}
int A_star(){//A*算法
   priority_queue<A>p;
    if(s==t) K++;
    if(dis[s]==INF)  return -1;
    A s1,s2;
    s1.from=s,s1.g=0;
    s1.f=s1.g+dis[s];
    p.push(s1);
    int tot=0;
    while(!p.empty()){
        s2=p.top();
        p.pop();
        if(s2.from==t){
            tot++;
            if(tot==K) return s2.g;

        }
        for(int i=head[s2.from];i!=-1;i=e[i].next){
            s1.from=e[i].to;
            s1.g=s2.g+e[i].w;
            s1.f=s1.g+dis[s1.from];
            p.push(s1);
        }
    }
    return -1;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    while(scanf("%d%d",&n,&m)!=EOF){
        scanf("%d%d%d%d",&s,&t,&K,&T);
        init();
        rep(i,1,m){int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
        }

        spfa();
        int ans=A_star();
        if(ans!=-1&&ans<=T)
            printf("yareyaredawa\n");
        else
            printf("Whitesnake!\n");
    }
    return 0;
 }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值