• poj[1734]


    跑floyd

    for(int k=1;k<=n;k++)
     for(int i=1;i<k;i++)
       for(int j=i+1;j<k;j++)
        min=dist[i,j]+a[i,k]+a[k,j];

    表示在k前,i,j间的最短路

    #include<map>
    #include<cmath>
    #include<ctime>
    #include<queue>
    #include<cstdio>
    #include<vector>
    #include<bitset>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    vector<int>lu;
    #define ll long long
    const ll inf=1e9;
    ll m,n,ans;
    ll d[120][120],pos[120][120],w[120][120];
    void mk(int x,int y){
        if(!pos[x][y])return ;
        mk(x,pos[x][y]);
        lu.push_back(pos[x][y]);
        mk(pos[x][y],y);
    }
    int main(){
        //freopen("p.in","r",stdin);
        ans=inf;
        ios::sync_with_stdio(false);
        cin>>n>>m;
        for(int i=1;i<=n;i++)
         {
         for(int j=1;j<=n;j++)
            d[i][j]=inf;
            d[i][i]=0;}
        for(int i=1;i<=m;i++){
        ll a,b,c;
        cin>>a>>b>>c;
        d[a][b]=d[b][a]=min(d[a][b],c);
        }
        for(int i=1;i<=n;i++)
         for(int j=1;j<=n;j++)
             w[i][j]=d[i][j];
        for(int k=1;k<=n;k++){
            for(int i=1;i<k;i++)
                for(int j=i+1;j<k;j++)
            if(d[i][j]+w[i][k]+w[k][j]<ans){
            lu.clear();
            lu.push_back(i);
            mk(i,j);
            lu.push_back(j);
            lu.push_back(k);
            ans=d[i][j]+w[i][k]+w[k][j];
            }
            for(int i=1;i<=n;i++)
             for(int j=1;j<=n;j++)
             if(d[i][j]>d[i][k]+d[k][j]){
             d[i][j]=d[i][k]+d[k][j];
             pos[i][j]=k;
             }
        }
        if(ans!=inf){for(int i=0;i<lu.size();i++)
        cout<<lu[i]<<' ';}
        else cout<<"No solution.";
        return 0;
    }
    View Code
    戒骄戒躁
  • 相关阅读:
    HDU 5444 Elven Postman 二叉排序树
    HDU 5438 Ponds dfs模拟
    Gym
    markdown test
    Gym
    集训回顾
    UVALive
    UVALive
    UVALive
    codeforcres 589 J
  • 原文地址:https://www.cnblogs.com/lxzl/p/9745035.html
Copyright © 2020-2023  润新知