# Ice_cream’s world II

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

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

/* ***********************************************
Author        :CKboss
Created Time  :2015年07月07日 星期二 16时55分48秒
File Name     :HDOJ2121.cpp
************************************************ */

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

using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn=1100;

int n,m;

struct Edge
{
int u,v,cost;
}edge[maxn*maxn];

int pre[maxn],id[maxn],vis[maxn];
int in[maxn];
int Root;

int zhuliu(int root,int n,int m,Edge edge[])
{
int res=0;
int v;
while(true)
{

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].cost<in[edge[i].v])
{
pre[edge[i].v]=edge[i].u;
in[edge[i].v]=edge[i].cost;
if(edge[i].u==root) Root=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++)
{
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].cost-=in[v];
}

n=tn;
root=id[root];
}
return res;
}

int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);

while(scanf("%d%d",&n,&m)!=EOF)
{
int sum=0;
int en=0;
Root=INF;

for(int i=0;i<m;i++)
{
scanf("%d%d%d",&edge[en].u,&edge[en].v,&edge[en].cost);
sum+=edge[en++].cost;
}

sum++;

for(int i=0;i<n;i++)
{
edge[en].u=n; edge[en].v=i; edge[en].cost=sum;
en++;
}

int lens = zhuliu(n,n+1,en,edge);

if(lens>=2*sum||lens==-1) puts("impossible");
else printf("%d %d\n",lens-sum,Root-m);

putchar(10);
}

return 0;
}


• 本文已收录于以下专栏：

## hdoj 2121 Ice_cream’s world II 【虚拟源点求无根树的最小树形图】

Ice_cream’s world II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth...

## hdu 2121 Ice_cream’s world II（无根结点最小树形图）

• acm_cxq
• 2016年05月11日 20:06
• 325

## HDU 2121 Ice_cream’s world II （不定根最小树形图求根）

• lwt36
• 2015年11月03日 23:21
• 245

## HDU 2121 Ice_cream’s world II(无固定根最小树形图、找最小根)

• Ramay7
• 2016年05月20日 23:49
• 222

## HDU 2121 Ice_cream’s world II 无固定根的最小树形图以及最小根

Problem Description After awarded lands to ACMers, the queen want to choose a city be her capital...

## HDU2121-Ice_cream’s world II(不定根的最小树形图)

• Lzedo
• 2016年08月27日 11:02
• 119

## [HDU 2121] Ice_cream’s world II 最小树形图

http://acm.hdu.edu.cn/showproblem.php?pid=2121题意：输入有 n 个点 m 条有向边，求图的最小生成树。思路：因为图中的边是有向边，所以题目要求的就是最小树...

## hdu2121 Ice_cream’s world II 【不定根的最小树形图】

**Ice_cream’s world II** Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32...

举报原因： 您举报文章：HDOJ 2121 Ice_cream’s world II 最小树形图无根树 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)