【Hrbust】 The Smallest Oil Tank (Kruskal算法)

D.The Smallest Oil Tank
Time Limit: 10000 MSMemory Limit: 65536 K
Total Submit: 136 (24 users)Total Accepted: 23 (18 users)Special Judge: No
Description

That’s great ! LPJ get married! He will take his wife on a honeymoon by driving. Now, he want to leave from home to Moscow. But his oil tank is limited and he doesn’t have enough oil to Moscow. Luckily,there are many gas stations.

There are n gas stations numbered from 0 to n-1. The 0 station is LPJ’s home and n-1 station is Moscow.  He knows the oil used from one gas station to another.  He can fill the tank at every gas station.

But the problem is coming. He want to take a car with the smallest oil tank.Please help LPJ get the smallest oil tank.

Input

There are multiple test cases.

Each test case contains two postive integers (n,m) separated by a space in one line. The n is the number of gas station.

The next m line ,each line contains three postive integers (u,v,w). It means that there is one road between u and v gas station using w Liter.

   There are at least one road from 0 to n-1 gas station.

The input will be terminated by the end of input file.

2<=n<=4,000

1<=m<=10,000,000

1<=w<=10,000,000

Output

For each test case ,output the smallest oil tank.

Sample Input
3 2
0 1 2
1 2 3
3 3
0 1 2
1 2 3
1 2 1

Sample Output
3
2

Author
ACgege



纯粹的Kruskal算法。

边的数量为10^7,用struct存边时,两个顶点的类型要是用int会爆内存,如下:

struct edge
{
    int x,y;//会爆内存
    int dis;
};

之后自己也没想到什么好的方法,后来请教师兄才知道可以把int改为short,这样就不会爆内存了:

struct edge
{
    short x,y;
    int dis;
};


代码:

#include<stdio.h>
#include<limits.h>
#include<algorithm>
#include<math.h>
#define N 4010
#define M 10000010
using namespace std;

struct edge
{
    short x,y;
    int dis;
};

short fa[N];
edge c2[M];
short find(short x)
{
    if(x!=fa[x])
        fa[x]=find(fa[x]);
    return fa[x];
}
void merge(short x,short y)
{
    fa[find(x)]=find(y);
}

bool cmp(edge p1,edge p2)
{
    return (p1.dis<p2.dis);
}


int main()
{
    int n,m,i,j,u,v,w;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=0;i<n;++i)
            fa[i]=i;

        for(i=0;i<m;++i)
        {
            scanf("%d%d%d",&u,&v,&w);
            c2[i].dis=w;
            c2[i].x=u;
            c2[i].y=v;
        }
        sort(c2,c2+m,cmp);
        for(i=0;i<m;++i)
        {
            if(c2[i].x==c2[i].y)
                continue;

            merge(c2[i].x,c2[i].y);
            if(find(0)==find(n-1))
            {

                 printf("%d\n",c2[i].dis);
                 break;
            }
        }

    }
    return 0;
}

官方题解给的是一个O(n^2)的算法,但是我没看太明白转移过程,先把代码贴上来吧:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 4000
#define inf 100000000

int dis[N][N];

int d[N];
bool vis[N];

int main()
{
//    freopen("d.in","r",stdin);
//    freopen("d.out","w",stdout);
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {

        for(int i=0;i<n;i++)
        for(int j=i;j<n;j++)
            dis[i][j]=dis[j][i]=inf;

        for(int i=0;i<m;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            dis[a][b]=dis[b][a]=min(dis[a][b],c);
        }

       for(int i=0;i<n;i++) d[i]=inf,vis[i]=0;
       d[0]=0;


       for(int i=0;i<n;i++)
       {
           int minn=inf;
           int v=-1;
           for(int j=0;j<n;j++)
             if(!vis[j] && minn>d[j])
           {
               minn=d[j];
               v=j;
           }
         if(v==-1 || v== n-1) break;
         vis[v]=1;

             for(int j=0;j<n;j++)
                  if(!vis[j] && max(d[v],dis[v][j]) < d[j])
                     d[j]=max(d[v],dis[v][j]);
       }

      printf("%d\n",d[n-1]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值