HDU - 2121 Ice_cream’s world II (不定根树形图)

Ice_cream’s world II

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6367    Accepted Submission(s): 1658


 

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

 

 

Author

Wiskey

 

 

Source

HDU 2007-10 Programming Contest_WarmUp

 

 

Recommend

威士忌

 

       大体题意就是让你看能否找到一个不定根的最小树形图。这样的话和多起点的最短路一样,虚拟一个超级源点,建一条比所有边总和sum多1的边,看最后的结果是否大于2*(sum+1),不是的话就是原图有最小树形图,是的话就代表没有。

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
struct fuck
{
	int from, to, len;
}ed[1000005];
int id[1005];
int pre[1005];
int vis[1005];
int in[1005];
int n, m, pos;
int zhuliu(int root)
{
	int res = 0;
	while (1)
	{
		for (int s = 0; s < n; s++)
			in[s] = inf;
		for (int s = 0; s < m; s++)
		{
			if (ed[s].from != ed[s].to&&in[ed[s].to] > ed[s].len)
			{
				in[ed[s].to] = ed[s].len;
				pre[ed[s].to] = ed[s].from;
				if (ed[s].from == root)
				{
					//	cout << s << " " << ed[s].from << " " << ed[s].to << endl;
					pos = s;
				}
			}
		}
		for (int s = 0; s < n; s++)
			if (in[s] == inf && s != root)
				return -1;
		int sum_huan = 0;
		in[root] = 0;
		memset(id, -1, sizeof(id));
		memset(vis, -1, sizeof(vis));
		for (int s = 0; s < n; s++)
		{
			res += in[s];
			int v = s;
			while (vis[v] != s&&id[v] == -1 && v != root)
			{
				vis[v] = s;
				v = pre[v];
			}
			if (v != root&&id[v] == -1)
			{
				for (int w = pre[v]; w != v; w = pre[w])
					id[w] = sum_huan;
				id[v] = sum_huan++;
			}
		}
		if (sum_huan == 0)
			break;
		for (int s = 0; s < n; s++)
		{
			if (id[s] == -1)
				id[s] = sum_huan++;
		}
		for (int s = 0; s < m; s++)
		{
			int v = ed[s].to;
			ed[s].from = id[ed[s].from];
			ed[s].to = id[ed[s].to];
			if (ed[s].from != ed[s].to)
			{
				ed[s].len -= in[v];
			}
		}
		n = sum_huan;
		root = id[root];
	}
	return res;
}
int main()
{
	while (~scanf("%d%d", &n, &m))
	{
		int k = m;
		int sum = 0;
		for (int s = 0; s < m; s++)
		{
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			ed[s].from = a, ed[s].to = b, ed[s].len = c;
			sum += c;
		}
		sum++;
		for (int s = 0; s < n; s++)
		{
			ed[m + s].from = n;
			ed[m + s].to = s;
			ed[m + s].len = sum;
		}
		m = m + n;
		n = n + 1;
		int ans = zhuliu(n-1);
		if (ans ==-1|| ans - sum >= sum)
		{
			printf("impossible\n\n");
		}
		else
		{
			printf("%d %d\n\n", ans - sum, pos - k);
		}
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值