• ACM-ICPC 2018 沈阳赛区网络预赛D(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 NN spots in the jail and MM roads connecting some of the spots. JOJO finds that Pucci knows the route of the former (K-1)(K−1)-th shortest path. If Pucci spots JOJO in one of these K-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 KK-th quickest path to get to the destination. What's more, JOJO only has TT units of time, so she needs to hurry.

    JOJO starts from spot SS, and the destination is numbered EE. 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 TT units of time.

    Input

    There are at most 5050 test cases.

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

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

    Then MM lines follows, each line containing three numbers U, VU,V and WW (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 UU-th spot to VV-th spot with time WW.

    It is guaranteed that for any two spots there will be only one directed road from spot AA to spot BB (1 leq A,B leq N, A eq B)(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).

    样例输入复制

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

    样例输出复制

    yareyaredawa

    题目来源

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

    题意:

        给你一个起始点S,一个终点E。有一个n个点,m条,每条边有一个时间W。问你走K短路的时间是否超过T。

    题目分析:

        第k短路的模板题。A*+SPFA即可。

    代码:

    #include <bits/stdc++.h>
    #define maxn 1005
    #define maxm 10005
    using namespace std;
    typedef long long ll;
    const ll INF = 0x3f3f3f3f3f3f3f3f;
    struct EdgeNode
    {
        int to;ll w;int next;
    }Edges[maxm],Edges1[maxm];
    struct edge{
        int to,next;
        ll cost;
    }q[maxm],q2[maxm];
    int head[maxn],head1[maxn];
    struct Node{
        int to;
        ll g,f;
        bool operator <(const Node &r) const{
            if(r.f == f)
                return r.g <g;
            return r.f<f;
        }
    };
    int vis[maxn];
    ll d[maxn];
    int A_Star(int start,int End,int N,int k){
        Node e,ne;
        int Cnt = 0;
        priority_queue<Node> que;
        if(start == End)
            k++;
        if(d[start] == INF)
            return -1;
        e.to=start;
        e.g=0;
        e.f=e.g+d[e.to];
        que.push(e);
        while(!que.empty())
        {
            e=que.top();
            que.pop();
            if(e.to==End)
                Cnt++;
            if(Cnt == k)
                return e.g;
            for(int i=head[e.to];i!=-1;i=q[i].next)
            {
                ne.to=q[i].to;
                ne.g=e.g+q[i].cost;
                ne.f=ne.g+d[ne.to];
                que.push(ne);
            }
        }
        return -1;
    }
    void SPFA(int s,int n)
    {
        //for(int i=0;i<=N;++i) d[i]=INF;
        memset(d,INF,sizeof(ll)*(n+1));
        memset(vis,0,sizeof(int)*(n+1));
        //memset(vis,0,sizeof(vis));
        vis[s]=1;
        d[s]=0;
        queue<int>que;
        que.push(s);
        while(!que.empty()){
            int to = que.front();
            que.pop();
            vis[to] = 0;
            for(int i=head1[to];i!=-1;i=q2[i].next){
                ll temp=d[to]+q2[i].cost;
                if(temp<d[q2[i].to]){
                    d[q2[i].to]=temp;
                    if(!vis[q2[i].to])
                    {
                        vis[q2[i].to] = 1;
                        que.push(q2[i].to);
                    }
                }
            }
        }
    }
    void add_edge1(int cntt,int from,int to,ll cost){
        q[cntt].to=to;
        q[cntt].cost=cost;
        q[cntt].next=head[from];
        head[from]=cntt;
    }void add_edge2(int cntt,int from,int to,ll cost){
        q2[cntt].to=to;
        q2[cntt].cost=cost;
        q2[cntt].next=head1[from];
        head1[from]=cntt;
    }
    void init(int n,int m){
        memset(q,0,sizeof(int)*(m+1));
        memset(q2,0,sizeof(int)*(m+1));
        memset(head,-1,sizeof(int)*(n+1));
        memset(head1,-1,sizeof(int)*(n+1));
    }
    int main()
    {
        int n,m,u,v,w,s,t,k,T;
        while(~scanf("%d%d",&n,&m)){
            scanf("%d%d%d%d",&s,&t,&k,&T);
            init(n,m);
            for(int i=0;i<m;i++){
                scanf("%d%d%d",&u,&v,&w);
                add_edge1(i,u,v,w);//正向建边
                add_edge2(i,v,u,w);//反向建边
            }
            SPFA(t,n);
            ll kthlenth = A_Star(s,t,n,k);
            if(kthlenth<=T&&kthlenth!=-1) puts("yareyaredawa");
            else puts("Whitesnake!");
            //printf("%lld
    ",kthlenth);
        }
        return 0;
    }
    
  • 相关阅读:
    Shell从入门到精通进阶之四:流程控制
    15个Python面试问题(附答案)
    python教程:内置函数和语法糖触发魔法方法
    python教程:利用while求100内的整数和
    python 教程:read(),readline() 和 readlines() 比较
    python生成随机数:uniform(), randint(), gauss(), expovariate()
    Python教程: 字符串转义序列及格式化
    python单例模式的五种实现方式
    Python NumPy的常用函数
    python五种调试或排错的方法
  • 原文地址:https://www.cnblogs.com/Chen-Jr/p/11007199.html
Copyright © 2020-2023  润新知