zoukankan      html  css  js  c++  java
  • $P1821 [USACO07FEB]银牛派对Silver Cow Party$

    spfa裸题
    跑两遍spfa

    #ifdef Dubug
    
    #endif
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL ;
    inline LL In() { LL res(0),f(1); register char c ;
    	while(isspace(c=getchar())) ; c == '-'? f = -1 , c = getchar() : 0 ;
    	while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(c=getchar())) ;
    	return res * f ;
    }
    
    int n , m , s ;
    const int N = 1000 + 5 ;
    const int M = 100000 + 5 ;
    const int Inf = INT_MAX ;
    struct node {
    	int v,w;
    	int nxt ;
    };
    int cnt(0) ;
    node edge[M<<1] ;
    int head[N] ;
    int dis[N] ;
    inline void Add(int u,int v,int w) {
    	edge[++cnt].v = v ;
    	edge[cnt].w = w ;
    	edge[cnt].nxt = head[u] ;
    	head[u] = cnt ;
    	return ;
    }
    
    int cnt2(0) ;
    node edge2[M<<1] ;
    int head2[N] ;
    int dis2[N] ;
    inline void _Add(int u,int v,int w) {
    	edge2[++cnt2].v = v ;
    	edge2[cnt2].w = w ;
    	edge2[cnt2].nxt = head2[u] ;
    	head2[u] = cnt2 ;
    	return ;
    }
    bool vis[N] ;
    queue< int > q;
    inline void Spfa(){
        for(register int i=1;i<=n;i++) dis[i] = Inf , vis[i] = 0 ;
        q.push(s) , dis[s] = 0 , vis[s] = 1 ;
        while(!q.empty()){
            int u=q.front() ;
            q.pop() , vis[u] = 0 ;
            for(register int i=head[u];i;i=edge[i].nxt){
                int v = edge[i].v;
                if(dis[v]>dis[u]+edge[i].w){
                    dis[v] = dis[u] + edge[i].w;
                    if(!vis[v]) vis[v] = 1 , q.push(v) ;
                }
            }
        }
        return ;
    }
    inline void Spfa2(){
        for(register int i=1;i<=n;i++) dis2[i] = Inf , vis[i] = 0 ;
        q.push(s) , dis2[s] = 0 , vis[s] = 1 ;
        while(!q.empty()){
            int u=q.front() ;
            q.pop() , vis[u] = 0 ;
            for(register int i=head2[u];i;i=edge2[i].nxt){
                int v = edge2[i].v;
                if(dis2[v]>dis2[u]+edge2[i].w){
                    dis2[v] = dis2[u] + edge2[i].w;
                    if(!vis[v]) vis[v] = 1 , q.push(v) ;
                }
            }
        }
        return ;
    }
    signed main() {
    	n = In() ; m = In() ; s = In() ;
    	for(register int i=1;i<=m;i++) {
    		int u , v , w ;
    		u = In() , v = In() , w = In() ;
    		Add(u,v,w) , _Add(v,u,w) ;
    	}
    	Spfa() , Spfa2() ;
    	int Max = -Inf ;
    	for(register int i=1;i<=n;i++) Max = max(Max,dis[i]+dis2[i]) ;
    	cout << Max << endl ;
    	return 0 ;
    }
    
    不存在十全十美的文章 如同不存在彻头彻尾的绝望
  • 相关阅读:
    PSP第二次总结
    周总结02
    四则运算2
    构建执法阅读笔记01
    周学习进度01
    暑假生活一
    构建之法阅读笔记03
    构建之法阅读笔记02
    个人课程总结
    软工大二下半年第十六周学习进度
  • 原文地址:https://www.cnblogs.com/qf-breeze/p/10627463.html
Copyright © 2011-2022 走看看