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

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

//http://blog.csdn.net/wsniyufang/article/details/6747604

/*
本题为不是固定根的最小树形图,我们可以虚拟出一根来,然后在把这个根跟每个点相连,相连的点可以设为无穷大,或者设为所有边和大一点,比如为r,然后就可以利用最小树形图进行计算了,计算出的结果减去r,如果比r还大就可以认为通过这个虚拟节点我们连过原图中两个点,即原图是不连通的,我们就可以认为不存在最小树形图。关于输出最小根也挺简单,在找最小入弧时,如果这条弧的起点是虚拟根,那么这条弧的终点就是要求的根。
*/
#include <cstdio>
#include <iostream>
#include<queue>
#include<set>
#include<ctime>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<cstring>
using namespace std;
const double eps=1e-10;
#define M 1009
#define type __int64
const type inf=(1LL)<<60;
struct point 
{
int x,y,h;
}p[M];


struct Node{
int u , v;
type cost;
}E[M*M+5];
int pre[M],ID[M],vis[M];
type In[M],sum; 
int n,m,ansi;
type Directed_MST(int root,int NV,int NE) {
type ret = 0;
while(true) {
//1.找最小入边
for(int i=0;i<NV;i++) In[i] = inf;
for(int i=0;i<NE;i++){
int u = E[i].u;
int v = E[i].v;
if(E[i].cost < In[v] && u != v) {
pre[v] = u;
if(u==root)//记录是root从哪一条边到有效点的(这个点就是实际的起点)
ansi=i;
In[v] = E[i].cost;
}
}
for(int i=0;i<NV;i++) {
if(i == root) continue;
if(In[i] == inf) return -1;//除了跟以外有点没有入边,则根无法到达它
}
//2.找环
int cntnode = 0;
memset(ID,-1,sizeof(ID));
memset(vis,-1,sizeof(vis));
In[root] = 0;
for(int i=0;i<NV;i++) {//标记每个环
ret += In[i];
int 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] = cntnode;
}
ID[v] = cntnode ++;
}
}
if(cntnode == 0) break;//无环
for(int i=0;i<NV;i++) if(ID[i] == -1) {
ID[i] = cntnode ++;
}
//3.缩点,重新标记
for(int i=0;i<NE;i++) {
int v = E[i].v;
E[i].u = ID[E[i].u];
E[i].v = ID[E[i].v];
if(E[i].u != E[i].v) {
E[i].cost -= In[v];
}
}
NV = cntnode;
root = ID[root];
}
return ret;
}
int main()
{
int u,v;
type w;
while(scanf("%d%d",&n,&m)!=EOF)
{
sum=0;
for(int i=0;i<m;i++)
{
scanf("%d%d%I64d",&u,&v,&w);
u++;
v++;
E[i].u=u;
E[i].v=v;
E[i].cost=w;
sum+=w;
}
sum++;
for(int i=m;i<m+n;i++)//第i条边的终点为i-m;
{
E[i].u=0;
E[i].v=i-m+1;
E[i].cost=sum;
}
type ans=Directed_MST(0,n+1,m+n);
if(ans==-1||ans-sum>=sum)//最小树形图的边权和值大于sum的两倍,说明不可能构成最小树形图,因为必定存在两条从虚拟点出发的边
puts("impossible");
else 
{ printf("%I64d %d\n",ans-sum,ansi-m);//利用i>=m时,第i条边的终点为i-m;这里不能替换为E[ansi].v-1;因为E[i]在朱刘算法过程中节点信息会变化。
}
puts("");
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值