hdu 2121(无根最小树形图)

Ice_cream’s world II

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Problem 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
解题思路:这道题是明显的最小树形图,只不过我们不知道根节点是谁,如果一个个去枚举肯定时间接受不了,所以这里采用了一种虚根的办法,建立一个虚根,并且将其与每个顶点连接起来,寻找以虚根为根节点的最小树形图。这里建立虚根还能想得通,但找实际的根节点就很难想了。这里记录的不是根节点,而是记录的虚边的序号,通过虚边的序号来找到根节点,详见代码。有点超时,还需改进。
</pre><br /><pre name="code" class="cpp">#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 10005;
struct Edge
{
	int u,v,c;
}edge[maxn];
int pos,in[maxn],id[maxn],vis[maxn],pre[maxn];

int Directed_MST(int root,int V,int E)
{
	int ans = 0,cnt;
	while(true)
	{
		//找最小入边
		memset(in,-1,sizeof(in));
		for(int i = 0; i < E; i++)
		{
			int u = edge[i].u;
			int v = edge[i].v;
			if((in[v] == -1 || in[v] > edge[i].c) && u != v)
			{
				in[v] = edge[i].c;
				pre[v] = u;
				if(u == root)
					pos = i;
			}
		}
		for(int i = 0; i < V; i++)
		{
			if(i == root) continue;
			if(in[i] == -1) return -1;
		}
		//缩点
		memset(vis,-1,sizeof(vis));
		memset(id,-1,sizeof(id));
		cnt = 0;
		in[root] = 0;
		for(int i = 0; i < V; i++)
		{
			ans += in[i];
			int u = i;
			while(vis[u] != i && id[u] == -1 && u != root)
			{
				vis[u] = i;
				u = pre[u];
			}
			if(u != root && id[u] == -1)
			{
				u = pre[u];
				while(u != i)
				{
					id[u] = cnt;
					u = pre[u];
				}
				id[i] = cnt++;
			}
		}
		if(cnt == 0) break;  //无环,break
		for(int i = 0; i < V; i++)
			if(id[i] == -1)
				id[i] = cnt++;
		//重构图
		for(int i = 0; i < E; i++)
		{
			int u = edge[i].u;
			int v = edge[i].v;
			edge[i].u = id[u];
			edge[i].v = id[v];
			if(id[u] != id[v])	//缩成了同一个点就不需要更新
				edge[i].c -= in[v];
		}
		V = cnt;
		root = id[root];
	}
	return ans;
}

int main()
{
	int sum,n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		sum = 0;
		for(int i = 1; i <= m; i++)
		{
			scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].c);
			sum += edge[i].c;
		}
		sum++;
		for(int i = 0; i < n; i++)
		{
			edge[m + i].u = n;
			edge[m + i].v = i;
			edge[m + i].c = sum;
		}
		int ans = Directed_MST(n,n + 1,n + m);
		//如果ans > 2*sum,说明一定会有两条虚边,这样就不符合只有一个根节点的题意了。
		//pos记录的不是顶点而是虚边的编号,由于顶点i所对应的虚边号为i+m,所以pos记录的是m+i
		//它所对应的顶点号是i,即pos-m
		if(ans == -1 || ans - sum >= sum) printf("impossible\n");
		else printf("%d %d\n\n",ans - sum,pos - m);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值