USACO07FEB银牛派对

【题意】n个点m条有向边,求单源最短路最长往返距离

【解题】建两个图

跑两遍spfa

不过有很多细节需要注意

#include<bits/stdc++.h>
const int N = 1005;
struct node{
    int v,w;
};
std::vector<node>g[3][N];

std::queue<int>q;
int n,k,m,x,y,z,hs,vis[N],f[3][N],ans = -0x3f3f;

void spfa(int k){
    
    memset(vis,0,sizeof vis);
    vis[hs] = 1;
    f[k][hs] = 0;
    q.push(hs);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = 0; 
        //std::cout << "DEBUG!"<<g[k][u].size();
        for(int i = 0;i < g[k][u].size();++i){
            int v = g[k][u][i].v, w = g[k][u][i].w;
            
            if(f[k][v] > f[k][u] + w){
                f[k][v] = f[k][u] + w;
                if(!vis[v]){
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
}


int main(){
  scanf("%d%d%d",&n,&m,&hs);
  memset(f,0x3f3f3f,sizeof(f));
  for(register int i = 1; i <= m; ++i){
      scanf("%d%d%d",&x,&y,&z);
      g[1][x].push_back((node){y,z});
      g[2][y].push_back((node){x,z});
  } 
  spfa(1);
  spfa(2);
  
  for(register int i = 1; i <= n; ++i){
       if(i == hs)continue;
       ans = std::max(ans,f[1][i] + f[2][i]);
  }
  printf("%d",ans);
  return 0;    
}
View Code

 

posted @ 2019-11-12 00:41  QUEKI嶺冬  阅读(131)  评论(0编辑  收藏  举报
/*! Color themes for Google Code Prettify | MIT License | github.com/jmblog/color-themes-for-google-code-prettify */ .pln{color:#4d4d4c}ol.linenums{margin-top:0;margin-bottom:0;color:#8e908c}li.L0,li.L1,li.L2,li.L3,li.L4,li.L5,li.L6,li.L7,li.L8,li.L9{padding-left:1em;background-color:#fff;list-style-type:decimal!important;}@media screen{.str{color:#718c00}.kwd{color:#8959a8}.com{color:#8e908c}.typ{color:#4271ae}.lit{color:#f5871f}.pun{color:#4d4d4c}.opn{color:#4d4d4c}.clo{color:#4d4d4c}.tag{color:#c82829}.atn{color:#f5871f}.atv{color:#3e999f}.dec{color:#f5871f}.var{color:#c82829}.fun{color:#4271ae}} /*下面是我设置背景色,字体大小和字体*/ .cnblogs-markdown code{ background:#fff!important; } .cnblogs_code,.cnblogs_code span,.cnblogs-markdown .hljs{ font-size:16px!important; } .syntaxhighlighter a, .syntaxhighlighter div, .syntaxhighlighter code, .syntaxhighlighter table, .syntaxhighlighter table td, .syntaxhighlighter table tr, .syntaxhighlighter table tbody, .syntaxhighlighter table thead, .syntaxhighlighter table caption, .syntaxhighlighter textarea { font-size: 16px!important; } .cnblogs_code, .cnblogs_code span, .cnblogs-markdown .hljs{ font-family:consolas, "Source Code Pro", monaco, monospace !important; } //以上是代码高亮 /* 文字特效 */