poj 3268 Silver Cow Party

跑两遍dij

其中第二遍把所有边反向

问题在于如何把各类边和dis清干净

2021 09 29 待补

2021 10 05 

#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=1e6;
int cnt=0,n,m,s,dis[maxn],vis[maxn],head[maxn],dis1[maxn],a[maxn],b[maxn],c[maxn];
struct mad{
    int to,val,next;
}edge[maxn];
struct lys
{
    int point,val;
    bool operator > (const lys tmp)const{
        return val>tmp.val;
    }
};
void add(int a,int b,int val)
{
    cnt++;
    edge[cnt].to=b;
    edge[cnt].val=val;
    edge[cnt].next=head[a];
    head[a]=cnt;
}
priority_queue<lys,vector<lys>,greater<lys> > q;
void dij(int s)
{
    lys son;
    son.point=s;
    son.val=0;
    //vis[s]=1;
    memset(dis,0x7f,sizeof(dis));
    dis[s]=0;
    
    q.push(son);
    
    while(!q.empty())
    {
        lys son=q.top();
        q.pop();
        
        int x=son.point,e=son.val;
        if(vis[x])
        {
            continue;
        }
        
        vis[x]=1;
        
        for(int i=head[x];i;i=edge[i].next)
        {
            if(dis[edge[i].to]>dis[x]+edge[i].val&&!vis[edge[i].to])
             {
                  dis[edge[i].to]=e+edge[i].val;
                  son.point=edge[i].to;
                  son.val=dis[edge[i].to];
                  q.push(son);
                  //printf("%d %d\n",son.point,son.val);
             }
        }
    }
}
void solve1( )
{
    for(int i=1;i<=m;i++)
     {
        add(a[i],b[i],c[i]);    
     } 
     dij(s);
}
void solve2( )
{   

    memset(vis,0,sizeof(vis));
    for(int i=1;i<=cnt;i++)
     {
         edge[i].to=0;
         edge[i].val=0;
         edge[i].next=0;
     }
     cnt=0;
    for(int i=1;i<=n;i++)
     {
         head[i]=0;
         dis1[i]=dis[i];
     }
     
     for(int i=1;i<=m;i++)
     {
         add(b[i],a[i],c[i]);
     }
     dij(s);
}
void solve3()
{   int maxn=-1;
    for(int i=1;i<=n;i++)
     {
         maxn=max(maxn,dis[i]+dis1[i]);
     }
     printf("%d",maxn);
}
int main()
{  //freopen("lyslyslys.in","r",stdin);


//n,m,x
//a to b
 
   

    cin>>n>>m>>s;
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        a[i]=x;
        b[i]=y;
        c[i]=z;
    }
    
    solve1();
    solve2();
    solve3();
/*    for(int i=1;i<=n;i++)
    {
        printf("%d ",dis[i]);
    }*/
}

 

posted @ 2021-09-29 19:08  liyishui  阅读(35)  评论(0编辑  收藏  举报