zoukankan      html  css  js  c++  java
  • hdu 1874 通畅工程续 夜

    http://acm.hdu.edu.cn/showproblem.php?pid=1874

    很水

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<ctime>
    #include<queue>
    #include<map>
    #include<cstring>
    #include<algorithm>
    #define LL long long
    using namespace std;
    
    const int N=205;
    const int MAX=0x5fffffff;
    int side[N][N];//边最大代表 没有连接 否则代表两点之间距离
    int dist[N];//到出发点的距离
    bool in[N];//是否已经最终更新 true 为已经最终更新   false 代表尚未最终更新
    int main()
    {
    
       //freopen("data.txt","r",stdin);
       int n,m;
       while(scanf("%d %d",&n,&m)!=EOF)
       {
           for(int i=0;i<n;++i)
           for(int j=0;j<n;++j)
           side[i][j]=MAX;//初始化
           while(m--)
           {
               int i,j,d;
               cin>>i>>j>>d;
               if(d<side[i][j])//为了防止有重边
               {
                   side[i][j]=side[j][i]=d;
               }
           }
           int S,T;
           cin>>S>>T;
           if(S==T)//特殊情况
           {
               cout<<"0"<<endl;
               continue;
           }
           for(int i=0;i<n;++i)
           dist[i]=side[S][i];//初始化各点到 出发点的距离
           memset(in,false,sizeof(in));
           in[S]=true;//标记 出发点已经更新
           for(int w=1;w<n;++w)//最多 n-1 次查询
           {
                int M=MAX,k=-1;
                for(int i=0;i<n;++i)
                {
                    if(in[i]==false&&dist[i]<M)//每次找还未被最终更新的点中  到出发点最短的点
                    {
                        M=dist[i];
                        k=i;
                    }
                }
                if(k==-1)//如果没找到 跳出循环
                break;
                if(k==T)//如果这个点就是终点 无需在继续寻找
                break;
                in[k]=true;
                for(int i=0;i<n;++i)
                {
                    if(in[i]==false)
                    {
                        if(dist[i]>dist[k]+side[k][i])//用目前最短的点去更新其它剩余的点
                        {
                            dist[i]=dist[k]+side[k][i];
                        }
                    }
                }
           }
           if(dist[T]==MAX)
           cout<<"-1"<<endl;
           else
           cout<<dist[T]<<endl;
       }
       return 0;
    }
    
  • 相关阅读:
    观后感
    用户故事排球教练助手
    本周工作量
    本周个人作业
    个人工作量
    个人作业
    产品计划总结
    典型用户和场景总结
    排球比赛计分规则
    PowerShell ISE:Windows Server 2008 R2默认不安装
  • 原文地址:https://www.cnblogs.com/liulangye/p/2623906.html
Copyright © 2011-2022 走看看