zoukankan      html  css  js  c++  java
  • BZOJ 1975: [Sdoi2010]魔法猪学院 大水题 第k短路 spfa

    https://www.lydsy.com/JudgeOnline/problem.php?id=1975

    我好像到现在了第k短路都不会写,mdzz。

    先spfa求出最短路,然后扫点存各种前置路径已经决定的最短路,小根堆暴力即可。

    有向图要存反向边,写完才发现的,临时添成两种了,丑也没办法

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<queue>
     7 using namespace std;
     8 #define pa pair<double,int>
     9 const int maxn=5010;
    10 const double inf=1e17;
    11 int n,m;double ene;
    12 struct nod{
    13     int y,next;double v;
    14 }e[2][maxn*40];
    15 int head[2][maxn]={},tot[2]={};
    16 double dis[maxn]={};
    17 bool vis[maxn]={};
    18 queue<int>q;
    19 priority_queue< pa , vector< pa > , greater< pa > >q1;
    20 void init(int x,int y,double v,int op){
    21     e[op][++tot[op]].v=v;e[op][tot[op]].y=y;e[op][tot[op]].next=head[op][x];head[op][x]=tot[op];
    22 }
    23 void spfa(){
    24     for(int i=1;i<=n;i++)dis[i]=inf;
    25     dis[n]=0;vis[n]=1;q.push(n);
    26     int x,y;double v;
    27     while(!q.empty()){
    28         x=q.front();q.pop();
    29         for(int i=head[0][x];i;i=e[0][i].next){
    30             y=e[0][i].y;v=e[0][i].v;
    31             //cout<<x<<y<<v<<dis[y]<<endl;
    32             if(dis[y]>dis[x]+v){
    33                 dis[y]=dis[x]+v;
    34                 if(!vis[y]){
    35                     vis[y]=1;
    36                     q.push(y);
    37                 }
    38             }
    39         }vis[x]=0;
    40     }
    41 }
    42 int getit(){
    43     q1.push(make_pair(dis[1],1));
    44     int ans=0;double vq,v;int x;
    45     while(!q1.empty()){
    46         x=q1.top().second; v=q1.top().first; q1.pop();
    47         vq=v-dis[x];
    48         if(x==n){
    49             if(ene-v>=0){ene-=v;ans++;}
    50             else break;
    51         }
    52         for(int i=head[1][x];i;i=e[1][i].next){
    53             q1.push(make_pair(vq+e[1][i].v+dis[e[1][i].y],e[1][i].y));
    54         }
    55     }
    56     return ans;
    57 }
    58 int main(){
    59     scanf("%d%d%lf",&n,&m,&ene);
    60     int x,y;double v;
    61     for(int i=1;i<=m;i++){scanf("%d%d%lf",&x,&y,&v);init(x,y,v,1);init(y,x,v,0);}
    62     spfa();
    63     printf("%d
    ",getit());
    64     return 0;
    65 }
    View Code

  • 相关阅读:
    unable to retrieve container logs for docker kubernetes
    Restart container within pod
    Kubernetes1.3:POD生命周期管理
    Options of the DB storage of prometheus
    prometheus重启hang住问题记录
    prometheus交流资源
    nc 从服务器上传下载文件
    负载均衡监控需求
    prometheus消耗内存问题
    10.Docker 镜像使用
  • 原文地址:https://www.cnblogs.com/137shoebills/p/8665050.html
Copyright © 2011-2022 走看看