现在的位置: 首页 > 综合 > 正文

hdu2121—Ice_cream’s world II

2019年02月16日 ⁄ 综合 ⁄ 共 3046字 ⁄ 字号 评论关闭

Ice_cream’s world II

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

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
 

Recommend
威士忌   |   We have carefully selected several similar problems for you:  2120 2122 1222 4009 2064

无固定根的最小树形图
加一个虚拟根,连到所有节点,边权设置为原图所有边的权值和+1 = sum,这样,如果最后求出来的ans-sum 大于等于sum,说明原图不连通,因为这时候虚拟根一定连了1个以上的节点
关于找真正的根,在朱刘算法寻找最小入边的时候搞定,注意边权,没告诉范围,用__int64

/*************************************************************************
    > File Name: hdu2121.cpp
    > Author: ALex
    > Mail: 405045132@qq.com 
    > Created Time: 2015年01月26日 星期一 14时46分50秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const __int64 inf = ((1LL) << 60);
const int N = 1110;

struct node
{
	int u, v;
	__int64 w;
}edge[N * N];

int g;
__int64 in[N];
int pre[N], vis[N], id[N];

__int64 zhuliu (int root, int n, int m)
{
	__int64 res = 0;
	int u, v;
	while (1)
	{
		for (int i = 0; i < n; ++i)
		{
			in[i] = inf;
		}
		for (int i = 0; i < m; ++i)
		{
			if (edge[i].u != edge[i].v && edge[i].w < in[edge[i].v])
			{
				in[edge[i].v] = edge[i].w;
				pre[edge[i].v] = edge[i].u;
				if (edge[i].u == root)
				{
					g = i;
				}
			}
		}
		for (int i = 0; i < n; ++i)
		{
			if (i != root && in[i] == inf)
			{
				return -1;
			}
		}
		int tn = 0;
		memset (id, -1, sizeof(id));
		memset (vis, -1, sizeof(vis));
		in[root] = 0;
		for (int i = 0; i < n; ++i)
		{
			res += in[i];
			v = i;
			while (vis[v] != i && id[v] == -1 && v != root)
			{
				vis[v] = i;
				v = pre[v];
			}
			if (v != root && id[v] == -1)
			{
				for (int u = pre[v]; u != v; u = pre[u])
				{
					id[u] = tn;
				}
				id[v] = tn++;
			}
		}
		if (tn == 0)
		{
			break;
		}
		for (int i = 0; i < n; ++i)
		{
			if (id[i] == -1)
			{
				id[i] = tn++;
			}
		}
		for (int i = 0; i < m; ++i)
		{
			int v = edge[i].v;
			edge[i].u = id[edge[i].u];
			edge[i].v = id[edge[i].v];
			if (edge[i].u != edge[i].v)
			{
				edge[i].w -= in[v];
			}
		}
		n = tn;
		root = id[root];
	}
	return res;
}

int main ()
{
	int n, m, u, v;
	__int64 w;
	while (~scanf("%d%d", &n, &m))
	{
		__int64 sum = 0;
		for (int i = 0; i < m; ++i)
		{
			scanf("%d%d%I64d", &u, &v, &w);
			if (u == v)
			{
				continue;
			}
			++u;
			++v;
			edge[i].u = u;
			edge[i].v = v;
			edge[i].w = w;
			sum += w;
		}
		++sum;
		for (int i = m; i < m + n; ++i)
		{
			edge[i].u = 0;
			edge[i].v = i - m + 1;
			edge[i].w = sum;
		}
		__int64 ans = zhuliu (0, n + 1, m + n);
		if (ans == - 1 || ans - sum >= sum)
		{
			printf("impossible\n");
		}
		else
		{
			printf("%I64d %d\n", ans - sum, g - m);
		}
		printf("\n");
	}
	return 0;
}

 

抱歉!评论已关闭.