2017-03-18 Ice_cream’s world II

本文探讨了有向图中的最小生成树问题,通过构造一个虚拟的根节点并使用朱刘算法来解决这一难题。文章提供了详细的算法实现过程及代码示例,并对比了使用Kruskal算法时的不同表现。

description

 After awarded lands to ACMers, the queen want to choose a city be her capital. This is an important event in ice_cream world, and it also a very difficult problem, because the world have N cities and M roads, every road was directed. Wiskey is a chief engineer in ice_cream world. The queen asked Wiskey must find a suitable location to establish the capital, beautify the roads which let capital can visit each city and the project’s cost as less as better. If Wiskey can’t fulfill the queen’s require, he will be punishing. 

Input

Every case have two integers N and M (N<=1000, M<=10000), the cities numbered 0…N-1, following M lines, each line contain three integers S, T and C, meaning from S to T have a road will cost C.

Output

If no location satisfy the queen’s require, you must be output “impossible”, otherwise, print the minimum cost in this project and suitable city’s number. May be exist many suitable cities, choose the minimum number city. After every case print one blank.

Sample Input

3 1
0 1 1

4 4
0 1 10
0 2 10
1 3 20
2 3 30

Sample Output

impossible

40 0

solution

求的是有向最小生成树,拟定一个虚根做朱刘算法,ans如果大于或等于2*sum+2,这意味着这个图本身(不包括虚根)时,是不连通的,所以输出impossible;其他则输出ans-sum和mr-m-1,mr是我们在做朱刘算法是记录下的最后一次离虚根最近的边的编号,-m是因为我们在建立虚根时,将虚根和每一个点连接起来了,-1是因为我们为了给这一题的虚根腾出位置,在存点的时候认为的给每一个输入点+1,因为原来的点可能会存在0;

Code

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#define ll long long
#define N 10001
#define bobo 100000000
using namespace std;
int n,m,cnt,sum,mr,tot,rec;
int co[N],fr[N],circ[N],num[N];
struct node{
  int fr,to,w;
}q[3*N];
int zhuliu(int sou,int whole)
  {
    rec=0;
    while(1)
      {
    memset(fr,-1,sizeof(fr));
    memset(circ,-1,sizeof(circ));
    memset(num,-1,sizeof(num));
    for (int i=0;i<=whole;++i) co[i]=bobo;
    for(int i=1;i<=m+n;i++)
      {
        int a=q[i].fr,b=q[i].to,c=q[i].w;
        if(c<co[b]&&a!=b)
          {
        co[b]=c;
        fr[b]=a;
        if(a==sou) mr=i;
          }
      }
    tot=0;
    co[sou]=0;
    for(int i=0;i<=whole;i++)
      {
        rec+=co[i];
        int bj=i;
        while(bj!=sou&&circ[bj]!=i&&num[bj]==-1) circ[bj]=i,bj=fr[bj];
        if(bj!=sou&&num[bj]==-1)
          {
        for(int hh=fr[bj];hh!=bj;hh=fr[hh]) num[hh]=tot;
        num[bj]=tot++;
          }
      }
    if(!tot) break;
    for(int i=0;i<=whole;i++) if(num[i]==-1) num[i]=tot++;
    for(int i=1;i<=m+n;i++)
      {
        int a=q[i].fr,b=q[i].to;
        q[i].fr=num[a];
        q[i].to=num[b];
        if(q[i].fr!=q[i].to) q[i].w-=co[b];
      }
    whole=tot-1;
    sou=num[sou];
      }
    return rec;
  }
int main()
{
  while(scanf("%d%d",&n,&m)!=EOF)
    {
      memset(q,-1,sizeof(q));
      int a,b,c;
      cnt=0;sum=0;
      for(int i=1;i<=m;i++)
    {
      scanf("%d%d%d",&a,&b,&c);
      q[i].fr=a+1,q[i].to=b+1,q[i].w=c;
      sum+=c;
    }
      sum++;
      for(int i=m+1;i<=m+n;i++)
    q[i].fr=0,q[i].to=i-m,q[i].w=sum;
      int ans=zhuliu(0,n);
      if(ans>=sum*2) printf("impossible\n\n");
      else printf("%d %d\n\n",ans-sum,mr-m-1);
    }
}

出于私心,我还要发一个没学朱刘算法前打的kruscal会超时(严重)的code

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#define ll long long
#define N 
using namespace std;
int n,m;
int fa[1001];
struct line
{
  int fr,to,w;
  bool operator <(const line &a) const
  {
    return w<a.w;
  }
}q[10001];
int findset(int x)
{
  return fa[x]==x?x:(fa[x]=findset(fa[x]));
}
int unionset(int x,int y)
{
  return fa[findset(y)]=findset(x);
}
int main()
{
  while(scanf("%d%d",&n,&m))
    {
      memset(fa,0,sizeof(fa));
      memset(q,0,sizeof(q));
      for(int i=0;i<n;i++)
    fa[i]=i;
      for(int i=0;i<m;i++)
    {
      scanf("%d%d%d",&q[i].fr,&q[i].to,&q[i].w);
    }
      sort(q,q+m);
      ll ans=0;
      for(int i=0;i<m;i++)
    {
      int fr=q[i].fr;
      int to=q[i].to;
      if(findset(fr)==findset(to)) continue;
      ans+=q[i].w;
      unionset(fr,to);
    }
      int pd=findset(0);
      bool falg=false;
      for(int i=1;i<n;i++)
    if(findset(i)!=pd) falg=true;
      if(!falg)
    printf("%lld %d\n",ans,findset(n-1));
      else
    printf("impossible\n");
    }
  return 0;
}

那个看朱刘算法的话,看这个
大概思想就是,不断的把途中的环进行缩点,然后更新点的编号和边权值,一直到没有一个环时,此时得到的ans就是有向的MST的总边权值;
http://blog.youkuaiyun.com/l123012013048/article/details/48375819

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值