• P1821 [USACO07FEB]银牛派对Silver Cow Party


    P1821 [USACO07FEB]银牛派对Silver Cow Party
    我当时看着邻接矩阵存的下,就想来一发floyed,然后发现了很多问题。
    floyed的判定条件非常严格,否则会挂。
    初始化为inf
    if(d[i][k]+d[k][j]<d[i][j]&&d[i][k]!=inf&&d[k][j]!=inf&&(i!=j&&i!=k&&j!=k))   80分

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<algorithm>
    #include<cmath>
    #include<ctime>
    #include<cstring>
    #define inf 2147483647
    #define For(i,a,b) for(register int i=a;i<=b;i++)
    #define p(a) putchar(a)
    #define g() getchar()
    
    using namespace std;
    int n,m,t,x,y,v,k;
    int d[1010][1010],ans=-9999999;
    
    void in(int &x)
    {
        char c=g();x=0;
        while(c<'0'||c>'9')c=g();
        while(c<='9'&&c>='0')x=x*10+c-'0',c=g();
    }
    void o(int x)
    {
        if(x>9)o(x/10);
        p(x%10+'0');
    }
    int main()
    {
         in(n),in(m),in(t);
         For(i,1,n)
           For(j,1,n)
             d[i][j]=inf;
         For(i,1,m)
         in(x),in(y),in(d[x][y]);
         For(k,1,n)
           For(i,1,n)
             For(j,1,n)
               if(d[i][k]+d[k][j]<d[i][j]&&d[i][k]!=inf&&d[k][j]!=inf&&(i!=j&&i!=k&&j!=k))
                    d[i][j]=d[i][k]+d[k][j];
               
         For(i,1,n)
             if(d[i][t]!=inf&&d[t][i]!=inf)
             ans=max(ans,d[i][t]+d[t][i]);
         o(ans);
         return 0;
    }

    正解是正常跑一遍spfa,然后反向建边,再跑一遍spfa,以x跑单源最短路径。

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<algorithm>
    #include<cmath>
    #include<ctime>
    #include<cstring>
    #define inf 2147483647
    #define For(i,a,b) for(register int i=a;i<=b;i++)
    #define p(a) putchar(a)
    #define g() getchar()
    
    using namespace std;
    int n,m,t,x,y,z,ans=-89998899;
    int d1[1010],d2[1010];
    bool vis[1010];
    queue<int>q;
    struct node
    {
        int n,v;
        node *next;
    }*e1[100010],*e2[100010];
    
    void in(int &x)
    {
        char c=g();x=0;
        while(c<'0'||c>'9')c=g();
        while(c<='9'&&c>='0')x=x*10+c-'0',c=g();
    }
    void o(int x)
    {
        if(x>9)o(x/10);
        p(x%10+'0');
    }
    
    void push(int x,int y,int v,node *&temp)
    {
        node *p;
        p=new node();
        p->n=y;
        p->v=v;
        if(temp==NULL)
        temp=p;
        else
        {
            p->next=temp->next;
            temp->next=p;
        }
    }
    
    void spfa(int x,int *d,node *e[])
    {
        d[x]=0;
        q.push(x);
        node *p;
        int t;
        while(q.size()>0)
        {
            t=q.front();
            p=e[t];
            vis[t]=true;
            while(p!=NULL)
            {
                if(d[t]+p->v<d[p->n])
                {
                    d[p->n]=d[t]+p->v;
                    if(!vis[p->n])
                    q.push(p->n);
                }
                p=p->next;
            }
            vis[t]=false;
            q.pop();
        }
    }
    
    int main()
    {
         in(n),in(m),in(t);
         For(i,1,m)
         {
             in(x),in(y),in(z);
             push(x,y,z,e1[x]);
             push(y,x,z,e2[y]);
         }
         For(i,1,n)
         d1[i]=inf,d2[i]=inf;
         spfa(t,d1,e1);
         spfa(t,d2,e2);
         For(i,1,n)
         ans=max(ans,d1[i]+d2[i]);
         o(ans);
         return 0;
    }
  • 相关阅读:
    shell-条件测试
    51Nod 1279 扔盘子 (思维+模拟)
    51Nod 1042 数字0-9的数量(数位DP)
    Codeforces 1138B Circus (构造方程+暴力)
    51nod 1133 不重叠的线段 (贪心,序列上的区间问题)
    51nod 1091 线段的重叠(贪心)
    EOJ Monthly 2019.2 E 中位数 (二分+中位数+dag上dp)
    牛客练习赛39 C 流星雨 (概率dp)
    牛客练习赛39 B 选点(dfs序+LIS)
    Educational Codeforces Round 57
  • 原文地址:https://www.cnblogs.com/war1111/p/7647276.html
Copyright © 2020-2023  润新知