ACM-ICPC 2018 沈阳赛区网络预赛 Made In Heaven 第k短路

1.题意:求第k短路,有向图

2.分析:直接写k短路超时(怎么写k短路看我上一篇文章)。。我:?????

把long long int改成lnt 以后。。过了。。过了。。了。。。

3.代码:

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long LL;
typedef pair<LL,int> P;
const int maxn = 1000 + 7;
struct Edge{
    int to,next;
    LL val;
}edge[maxn*10];
struct Line{
    int to,next;
    LL val;
}line[maxn*10];
int n,m,s,e,tot,k,head[maxn],revhead[maxn];
int tot2;
bool vis[maxn];
LL dist[maxn];
LL tim;
inline void addEdge(int a,int b,LL c){
    edge[tot].to = b;edge[tot].next =  head[a];edge[tot].val = c;head[a] = tot++;
}
inline void AddEdge(int a,int b,LL c){
   line[tot2].to = b;line[tot2].next = revhead[a];line[tot2].val = c;revhead[a] = tot2++;
}
struct Node{
   int to;
   LL cost;
   bool operator <(const Node&another)const{
        return cost + dist[to] > another.cost + dist[another.to];
   }
   Node(int a,LL c):to(a),cost(c) {}
};
inline void Dijkstra(int a){
   dist[a] = 0;
   priority_queue<P,vector<P>,greater<P> >que;
   que.push(P(0,a));
   while(!que.empty()){
       P p = que.top();
       que.pop();
       if(vis[p.second])continue;
       vis[p.second] = 1;
       LL num = p.first;
       for(int i = revhead[p.second];~i;i = line[i].next){
           if(!vis[line[i].to]&&dist[line[i].to] > num + line[i].val){
               dist[line[i].to] = num + line[i].val;
               que.push(P(dist[line[i].to],line[i].to));
           }
       }
   }
}
inline LL BFS(int a){
   priority_queue<Node> que;
   que.push(Node(a,0));
   while(!que.empty()){
      Node node = que.top();
      que.pop();
      if(node.to==e){
         k--;
         if(k==0){
            return node.cost;
         }
      }
      for(int i = head[node.to];~i;i = edge[i].next){
           que.push(Node(edge[i].to,node.cost + edge[i].val));
      }
   }
   return -1;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF){
        if(m==0){printf("Whitesnake!\n");continue;}
        tot = tot2 = 0;
        memset(dist,INF,sizeof(dist));
        memset(vis,0,sizeof(vis));
        memset(head,-1,sizeof(head));
        memset(revhead,-1,sizeof(revhead));
        scanf("%d%d%d%lld",&s,&e,&k,&tim);
        for(int i = 0;i<m;i++){
            int a,b;
            LL v;
            scanf("%d%d%lld",&a,&b,&v);
            addEdge(a,b,v);
            AddEdge(b,a,v);
        }
        Dijkstra(e);
        if(dist[s]==INF){
            printf("Whitesnake!\n");
            continue;
        }
        LL ans = BFS(s);
        if(ans>tim||ans==-1)printf("Whitesnake!\n");
        else printf("yareyaredawa\n");
    }
    return 0;
}


 

猜你喜欢

转载自blog.csdn.net/qq_40772692/article/details/82532630