码迷,mamicode.com
首页 > 其他好文 > 详细

uva 10457(最小瓶颈路)

时间:2017-11-09 19:41:12      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:最小   code   stream   比赛   algo   struct   while   max   告诉   

比赛的时候读错题了,赛后非要建最小生成树然后dfs暴搜,有人告诉我不行,我还非要改一遍,改了一年,想明白了,不能保证下限,比如2,3,5能使两个点连同,4,5也能的话,就不对了,想想我也是个铁头娃

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
const int maxv=200+10;
const int maxn=1000+100;
const int inf=0x3f3f3f3f;
int c[maxv];
int vis[maxv];
int maxcost[maxv][maxv];
int n,m,k;
int s,t;
int found(int x)
{
     if(x==c[x]) return x;
     else return c[x]=found(c[x]);
}
bool same(int x,int y)
{
    if(found(x)==found(y))
    {
        return 1;
    }
    else return 0;
}
void unit(int x,int y)
{
    x=found(x);
    y=found(y);
    if(x==y) return;
    c[x]=y;
}
struct note
{
    int u;
    int v;
    int len;
    bool operator <(const note &p) const
    {
        return len<p.len;
    }
}aa[maxn];
void init()
{
    for(int i=0;i<=n;i++)
        c[i]=i;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=1;i<=m;i++)
            scanf("%d%d%d",&aa[i].u,&aa[i].v,&aa[i].len);
         sort(aa+1,aa+1+m);
        int nn,mm,k;
        scanf("%d%d",&nn,&mm);
        scanf("%d",&k);
       for(int i=1;i<=k;i++)
       {
          scanf("%d%d",&s,&t);
            int ans=inf;
          for(int i=1;i<=m;i++)
          {
              init();
              for(int j=i;j<=m;j++)
              {
                  if(!same(aa[j].u,aa[j].v))
                  {
                      unit(aa[j].v,aa[j].u);
                      if(same(s,t))
                      {
                         ans=min(ans,aa[j].len-aa[i].len);
                         break;
                      }
                  }
              }

          }
          printf("%d\n",ans+nn+mm);
       }
    }
    return 0;
}

 

uva 10457(最小瓶颈路)

标签:最小   code   stream   比赛   algo   struct   while   max   告诉   

原文地址:http://www.cnblogs.com/Wangwanxiang/p/7810871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!