zoukankan      html  css  js  c++  java
  • poj 3268 最短路

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    using namespace std;
    
    const int maxn = 1005;
    const int maxm = 100005;
    const int INF = 0x3f3f3f3f;
    int N,M,X;
    int first[maxn];
    int en;
    int d[maxn],d1[maxn];
    struct Edge{
        int u,v,w;
        int next;
        void assign(int a,int b,int c,int d){
            u = a;  v = b;  w = c;  next = d; 
        }
        bool operator <(const Edge& r)const {
            return w > r.w;
        }
    }edge[maxm];
    void Addedge(int a,int b,int c){
        int d = first[a];
        first[a] = en;
        edge[en++].assign(a,b,c,d);
    }
    
    void Dijkstra(int x,int n){
        priority_queue <Edge> Q;
        while(!Q.empty())  Q.pop(); 
        memset(d,0x3f,sizeof(d));
        int vis[maxn];
        memset(vis,0,sizeof(vis));
        Q.push((Edge){x,0,0});  d[x] = 0; 
        while(!Q.empty()){ 
            Edge e = Q.top() ; Q.pop() ; 
            int u = e.u; 
            if(vis[u]) continue;
            vis[u] = true;  
            for(int i=first[u];i!=-1;i=edge[i].next){  
                int v = edge[i].v;     
                if( d[v] > d[u] + edge[i].w){
                    d[v] = d[u] + edge[i].w;    
                    Q.push((Edge){v,0,d[v]}); 
                }
            }
        }
        
    }
    /*
    void SPFA(int x,int n){
        queue<int> Q;
        memset(d,0x3f,sizeof(d));
        bool inq[maxn];
        memset(inq,0,sizeof(inq));
        int cnt[maxn];
        memset(cnt,0,sizeof(cnt));
        Q.push(x); inq[x] = true; cnt[x]++; d[x] = 0; 
        while(!Q.empty()){ 
            int u = Q.front() ; Q.pop() ; inq[u] = false;
            for(int i=first[u];i!=-1;i=edge[i].next){  
                int v = edge[i].v;      // printf("%d %d \n",v,inq[v]);
                if( d[v] > d[u] + edge[i].w){
                    
                    d[v] = d[u] + edge[i].w;    //printf("%d %d %d\n",v,u,d[v]);
                    if(!inq[v]){
                     inq[v] = true;
                     Q.push(v);
                     cnt[v]++;    
                    }
                    if(cnt[v] > n) return;
                }
                
            }
        }
        
        
    }*/
    void init(){
       memset(first,-1,sizeof(first));
       en = 1;    
    }
    
    int main()
    {
        //if(freopen("input.txt","r",stdin)== NULL)  {printf("Error\n"); exit(0);}
        
        cin>>N>>M>>X;
        int u[maxm],v[maxm],w[maxm];
        init();
        for(int i=1;i<=M;i++){
            cin>>u[i]>>v[i]>>w[i];
            Addedge(u[i],v[i],w[i]);
        }
        //SPFA(X,N);
        Dijkstra(X,N);
        for(int i=1;i<=N;i++)  d1[i] = d[i]; 
        init(); 
        for(int i=1;i<=M;i++){
            Addedge(v[i],u[i],w[i]);  
        }
        //SPFA(X,N);
        Dijkstra(X,N);
        int ans = 0;  
        for(int i=1;i<=N;i++)   ans = max(ans ,d[i] + d1[i]);
        printf("%d\n",ans);
    }
  • 相关阅读:
    CSS揭秘(六用户体验)
    CSS揭秘(五字体排印)
    CSS揭秘(四视觉效果)
    java split() 使用 . 来分割,转义(“” "|" "*" "+")
    springboot get请求405 Method Not Allowed
    websocket前端消息读取问题
    java解决中文乱码问题(jar包运行时中文返回前端数据或者控制台输出乱码问题)
    解决mysql 允许执行 XA RECOVER语句(atomikos 解决分布式事务报错)
    navicat修改mysql密码
    mybatisplus添加字段填充
  • 原文地址:https://www.cnblogs.com/acmdeweilai/p/3112765.html
Copyright © 2011-2022 走看看