hdu2121---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): 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

HDU 2007-10 Programming Contest_WarmUp

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: [email protected]
    > 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;
}
时间: 2024-10-17 13:42:05

hdu2121---Ice_cream’s world II的相关文章

HDU2121 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): 2744    Accepted Submission(s): 630 Problem Description After awarded lands to ACMers, the queen want to choose a city be he

HDU2121 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): 3005    Accepted Submission(s): 704 Problem Description After awarded lands to ACMers, the queen want to choose a city be he

HDU2121 Ice_cream’s world II —— 最小树形图 + 超级点

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 Ice_cream's world II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5832    Accepted Submission(s): 1493 Problem Description After awarded la

hdu2121 - Ice_cream’s world II(朱刘算法,不固定根)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目意思大概是要你在一些城市中选一个做首都 , 要求首都都能到其他城市 , 道路花费要最少 , 且道路都是单向的 , 这个时候就要用到最小树形图算法了 , 而且是不固定根. 不定根就是加一个虚根(原本不存在的点) , 可以让这个虚根到每个点的距离大于原本所有点连接的道路花费之和sum , 然后计算出的结果减去sum,如果比sum还大就可以认为通过这个虚拟节点我们连过原图中两个点,即原图是不连通

hdu2121 Ice_cream’s world II 最小树形图(难)

这题比HDU4009要难一些.做了4009,大概知道了最小树形图的解法.拿到这题,最直接的想法是暴力.n个点试过去,每个都拿来做一次根.最后WA了,估计是超时了.(很多题都是TLE说WA,不断修改代码也看不出来错哪了). 网上的正解是添加一个虚拟根(树根),使得它与n个点都有边相连.HDU4009题这样得到的边有实际的意义,好理解.这题这样得到的边不好理解,并且边权应该是多少也有讲究.别人的解题报告中是把这些边的权值设为原本所有边的权值之和加1.这样做的目的,是为了完全把这些边与原本的边区分开.

HDOJ 2121 Ice_cream’s world II 最小树形图无根树

朱刘算法 最小树形图无根树: 建立一个虚拟的根节点,向所有节点连边,权值为其他所有边的权值和+1 在求最小树形图的时候,记录和虚拟的根相连的是哪个节点 在这题中,边是从小往大加的所以直接记录的是相连的是第几号边.... Ice_cream's world II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3442    Accept

hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】

称号:pid=2121" target="_blank">hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向图.然后找一个点.到全部点的距离和最小.找出这个点并输入距离. 分析:非常明显是求一个最小树形图,可是没有说根节点.要找跟节点,我们能够虚拟一个节 点 x .x 到全部节点连边距离为前面全部距离和+1为 dis . 然后从x 节点求一次最小树形图为ans,则ans - dis 就是最小树形图的距离. 假设图不

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): 3115    Accepted Submission(s): 737 Problem Description After awarded lands to ACMers, the queen want to choose a city be her

hdoj 2121 Ice_cream’s world II 【无根节点最小树形图】

题目:hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向图,然后找一个点,到所有点的距离和最小,找出这个点并输入距离. 分析:很明显是求一个最小树形图,但是没有说根节点,要找跟节点,我们可以虚拟一个节 点 x ,x 到所有节点连边距离为前面所有距离和+1为 dis . 然后从x 节点求一次最小树形图为ans,则ans - dis 就是最小树形图的距离. 如果图不连通,或者ans>=2*dis 说明不存在,都则与 x 点出发的边就是结果点 A

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

题目地址:HDU 2121 这题没有给定根.最容易想到的当然是暴力,枚举所有的根,但是TLE是显然的..为了处理不定根的情况,可以虚拟一个根,然后用这个根去跟所有的点连边,权值为其他所有权值的和+1,目的是防止成为最小树形图的一条边.然后跑出最小树形图后,那么这个虚拟根肯定跟一个实际根相连,这时候根就找到了,然后再在最终的总花费中减去虚拟的那条边的权值就可以了. 代码如下: #include <iostream> #include <string.h> #include <m