zoukankan      html  css  js  c++  java
  • 次小生成树

    学习了一种简单的n2复杂度的最小生成树算法。
    先求出最小生成树,dfs每个节点算出f[i][j](表示i到j的路径中长度最长的边的长度),枚举所有不在生成树上的边(i,j),如果把这条边加进去一定会成环,删掉f[i][j],算出与原来最小生成树的权值和之差,取最小的差,即为最小生成树的大小。
     
    The Unique MST

    Description

    Given a connected undirected graph, tell if its minimum spanning tree is unique. 

    Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties: 
    1. V' = V. 
    2. T is connected and acyclic. 

    Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'. 

    Input

    The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

    Output

    For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

    Sample Input

    2
    3 3
    1 2 1
    2 3 2
    3 1 3
    4 4
    1 2 2
    2 3 2
    3 4 2
    4 1 2
    

    Sample Output

    3
    Not Unique!

    次小生成树与最小生成树一样即不唯一,注意判断原图为一棵树的情况。
    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstdio>
    #define nn 110
    #define mm 20010
    using namespace std;
    int mst=0,e=0,n,m,fa[nn],fir[nn],nxt[mm],to[mm],w[mm],f[nn][nn];
    bool vis[nn];
    int read()
    {
    	int ans=0,f=1;char ch=getchar();
    	while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
    	while(isdigit(ch)) {ans=ans*10+ch-'0';ch=getchar();}
    	return ans*f;
    }
    struct bb{
    	int fro,to,w;
    	bool use;
    }g[mm];
    int find(int x)
    {
    	return fa[x]==x? x:fa[x]=fa[fa[x]];
    }
    void add(int a,int b,int ww)
    {
    	nxt[++e]=fir[a];fir[a]=e;to[e]=b;w[e]=ww;
    	nxt[++e]=fir[b];fir[b]=e;to[e]=a;w[e]=ww;
    }
    void kruskal()
    {
    	int sum=0;e=0;mst=0;
    	for(int i=1;i<=n;i++)
    	  fa[i]=i;	
    	memset(fir,0,sizeof(fir));
        memset(nxt,0,sizeof(nxt));
    	for(int i=1;i<=m;i++)
    	  if(find(g[i].fro)!=find(g[i].to))
    	  {
    	  	fa[fa[g[i].fro]]=fa[g[i].to];
    	  	add(g[i].fro,g[i].to,g[i].w);
    	  	mst+=g[i].w;
    		g[i].use=1;           ///////
    	  	if(++sum==n-1)
    	  	  break;
    	  }
    }
    void dfs(int o,int s)
    {
    	vis[o]=1;
    	for(int i=fir[o];i;i=nxt[i])
    	  if(!vis[to[i]])
    	  {
    	  	f[s][to[i]]=max(f[s][o],w[i]);
    	  	dfs(to[i],s);
    	  }
    }
    void les()
    {
    	int more=0,cha=100000000;
    	for(int i=1;i<=m;i++)
    	  if(!g[i].use)
    	  {
    	  	more++;
    	  	cha=min(cha,g[i].w-f[g[i].fro][g[i].to]);
    	  }
    	if(!more)
    	  printf("%d
    ",mst);
    	else if(!cha)
    	  printf("Not Unique!
    ");
    	else
    	  printf("%d
    ",mst);
    }
    int main()
    {
    	int t;
    	t=read();
    	while(t--)
    	{
    		n=read();m=read();
    		for(int i=1;i<=m;i++)
    		{
    			g[i].fro=read();g[i].to=read();g[i].w=read();g[i].use=0;
    		}
    		kruskal();
    		for(int i=2;i<=n;i++)
    	      vis[i]=0;
    	    for(int i=1;i<=n;i++)
    		{
    			for(int j=1;j<=n;j++)
    	          vis[j]=0;
    	        dfs(i,i);
    		}
    		les();
    	}
    	return 0;
    }
     
  • 相关阅读:
    jmeter_04_常用取样器
    jmeter_03_鉴权
    jmeter_02_目录文档说明
    jmeter_01_常用快捷键
    Web Api 与 Andriod 接口对接开发经验
    Eclipse自动生成作者、日期注释等功能设置
    c#解析XML到DATASET及dataset转为xml文件函数
    Jquery 仿 android Toast效果
    正在运行的android程序,按home键之后退回到桌面,在次点击程序图标避免再次重新启动程序解决办法
    异步网络加载开源框架AsyncHttpClient使用
  • 原文地址:https://www.cnblogs.com/charlotte-o/p/7577322.html
Copyright © 2011-2022 走看看