Made In Heaven 2018 沈阳赛区网络预赛 D题

求第k短路

模板题 套模板即可

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>

using namespace std;

const int maxn=1005;
const int maxe=100005;

struct State{
    int f;  // f=g+dis dis表示当前点到终点的最短路径,即之前的预处理
    int g; //g表示到当前点的路径长度
    int u;
    bool operator<(const State b)const{
        if(f==b.f)return g>b.g;
        return f>b.f;
    }
};
struct Edge{
    int v;
    int w;
    int next;
}edge[maxe],reedge[maxe];
int head[maxn],rehead[maxn];
int dis[maxn],vis[maxn];
int n,m;
int cot;
int s,t,k;

void init(){
    cot=0;    //cot代表边的id  每条边的id都不相同
    memset(head,-1,sizeof(head));
    memset(rehead,-1,sizeof(rehead));
}

void addedge(int u,int v,int w){
    edge[cot].v=v;
    edge[cot].w=w;
    edge[cot].next=head[u];//记录上一次u的id是多少 这样方便遍历  初始值为-1
    head[u]=cot;//head[u]  给这个u标记上独一无二的id
    reedge[cot].v=u;
    reedge[cot].w=w;
    reedge[cot].next=rehead[v];
    rehead[v]=cot++;
}

void SPFA(){
    queue<int>q;
    memset(vis,0,sizeof(vis));
    memset(dis,-1,sizeof(dis));
    int u,v;
    q.push(t);
    vis[t]=true;//vis表示当前点是否在队列
    dis[t]=0;
    while(!q.empty()){
        u=q.front();
        q.pop();
        vis[u] = 0;
      //rehead[u] 是u最后一次出现的id  reedge[i].ne  代表第i次出现的边上一次出现的id
        for(int i=rehead[u];~i;i=reedge[i].next){  //~i取反 当i为-1时正好取反为0 退出for
            v=reedge[i].v;
            if(dis[v]>dis[u]+reedge[i].w || dis[v]==-1){
                dis[v]=dis[u]+reedge[i].w;
                if(!vis[v]){
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
}

int Astart(){
    if(s==t)k++;
    if(dis[s]==-1)return -1;
    int cnt=0;
    priority_queue<State>q; // 优先队列
    State a,b;
    a.g=0;
    a.u=s;
    a.f=a.g+dis[a.u];
    q.push(a);
    while(!q.empty()){
        b=q.top();
        q.pop();
        if(b.u==t){
            cnt++;
            //printf("%d %d %d %d\n",b.f,b.g,b.u,dis[b.u]);
            if(cnt==k)return b.g;
        }
        for(int i=head[b.u];~i;i=edge[i].next){
            a.g=b.g+edge[i].w;
            a.u=edge[i].v;
            a.f=a.g+dis[a.u];
            q.push(a);
        }
    }
    return -1;
}

int main()
{
    int u,v,w,T;
    while(scanf("%d%d",&n,&m)==2){
        init();
        scanf("%d%d%d%d",&s,&t,&k, &T);  //起点 终点 第k条 时间T
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
        }
        SPFA();

        if (Astart() <= T && Astart() != -1) //小于等于T 并且有路才可以
            printf("yareyaredawa\n");
        else printf("Whitesnake!\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/WTSRUVF/p/9610285.html

时间: 2024-10-12 20:25:24

Made In Heaven 2018 沈阳赛区网络预赛 D题的相关文章

ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph

"Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a bipartite graph is a fantastic graph. He has two fantastic numbers, and he wants to let all the degrees to between the two boundaries. You can pick up sev

ACM-ICPC 2018 沈阳赛区网络预赛 K. Supreme Number

A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying two smaller natural numbers. Now lets define a number NN as the supreme number if and only if each number made up of an non-empty subsequence of all

【ACM-ICPC 2018 沈阳赛区网络预赛 I】Lattice&#39;s basics in digital electronics

[链接] 我是链接,点我呀:) [题意] 每个单词的前缀都不同. 不能更明示了... 裸的字典树. 模拟一下.输出一下就ojbk了. [题解] #include <bits/stdc++.h> #define LL long long #define rep1(i,a,b) for (int i = a;i <= b;i++) #define rep2(i,a,b) for (int i = a;i >= b;i--) #define all(x) x.begin(),x.end(

ACM-ICPC 2018 南京赛区网络预赛 E题

ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest with n (0 < n \le 20)n(0<n≤20) problems. And he knows the answer of all of these problems. However, he can submit ii-th problem if and only if he has s

ACM-ICPC 2018 焦作赛区网络预赛 H题 String and Times(SAM)

Now you have a string consists of uppercase letters, two integers AA and BB. We call a substring wonderful substring when the times it appears in that string is between AA and BB (A \le times \le BA≤times≤B). Can you calculate the number of wonderful

ACM-ICPC 2018 焦作赛区网络预赛 B题 Mathematical Curse

A prince of the Science Continent was imprisoned in a castle because of his contempt for mathematics when he was young, and was entangled in some mathematical curses. He studied hard until he reached adulthood and decided to use his knowledge to esca

ACM-ICPC 2018 焦作赛区网络预赛 L 题 Poor God Water

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous. Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 33 

ACM-ICPC 2018 焦作赛区网络预赛 K题 Transport Ship

There are NN different kinds of transport ships on the port. The i^{th}ith kind of ship can carry the weight of V[i]V[i] and the number of the i^{th}ith kind of ship is 2^{C[i]} - 12C[i]?1. How many different schemes there are if you want to use thes

【 ACM-ICPC 2018 沈阳赛区网络预赛 D】Made In Heaven

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 点可以重复走的k短路. [代码] #include <bits/stdc++.h> #define LL long long #define rep1(i,a,b) for (int i = a;i <= b;i++) #define rep2(i,a,b) for (int i = a;i >= b;i--) #define all(x) x.begin(),x.end() #define pb push_bac