【HDU2121】【有向图的最下生成树】【无固定跟节点】

时间:2022-04-26 12:57:02

Ice_cream’s world II

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


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
威士忌
 





#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;


int n,m;
typedef long long ll;
struct Edge
{
	int a,b;
	ll c;
}edge[1005*1005];
//int len;
int pos;

ll d[2020];
int p[2020];
int vis[2020];
int id[2020];
ll zhuli(int root,int n,int m,Edge edge[])
{
	ll ret = 0;
	while(true)
	{
		for(int i=0;i<n;i++)
		{
			d[i] = 0x7f7f7f7f;
			p[i] = -1;
		}

		for(int i=0;i<m;i++)
		{
			Edge e = edge[i];
			if(e.a != e.b && d[e.b] > e.c)
			{
				d[e.b] = e.c;
				p[e.b] = e.a;
				if(e.a == root)
				{
					pos = i;
				}
			}
			
		}

		for(int i=0;i<n;i++)
		{
			if(i != root && p[i] == -1) return -1;
		}

		int cnt = 0;
		d[root] = 0;

		for(int i=0;i<n;i++)
		{
			id[i] = -1;
			vis[i] = -1;
		}

		for(int i=0;i<n;i++)
		{

			ret += d[i];

			int v = i;
			while(v != -1 && v!= root && vis[v] != i && id[v] == -1)
			{
				vis[v] = i;
				v = p[v];
			}

			if(v!=-1 && v != root && id[v] == -1)
			{
				int tt = v;
				do
				{
					id[tt] = cnt;
					tt = p[tt];
				}while(tt != v);

				cnt ++;
			}
		}

		if(cnt == 0) break;
		for(int i=0;i<n;i++)
		{
			if(id[i] == -1) id[i] = cnt ++;
		}

		for(int i=0;i<m;i++)
		{
			int v = edge[i].b;
			edge[i].a = id[edge[i].a];
			edge[i].b = id[edge[i].b];
			if(edge[i].a != edge[i].b)
			{
				edge[i].c -= d[v];
			}
		}

		n = cnt;
		root = id[root];
	}
	return ret;
}

int main()
{
	while(scanf("%d%d",&n,&m) != EOF)
	{
		//len = 0;
		ll sum = 0;
		for(int i=0;i<m;i++)
		{
			int a,b;
			ll c;
			scanf("%d%d%I64d",&a,&b,&c);
			a ++;
			b ++;
			//if(a == b) continue;
			edge[i].a = a;
			edge[i].b = b;
			edge[i].c = c;
			sum += c;
			//len ++;
		}
		int tmp_m = m;
		sum ++;
		for(int i=m;i<n+m;i++)
		{
			edge[i].a = 0;
			edge[i].b = i - m + 1;
			edge[i].c = sum;
			//len ++;
		}
		//n ++;
		ll ans = zhuli(0,n+1,n+m,edge);

		//cout << pos << endl;
		if(ans == -1 || ans - sum >= sum)
		{
			puts("impossible");
		}
		else
		{
			printf("%I64d %d\n",ans-sum,pos-m);
		}

		puts("");
	}
	
	return 0;
}