开始学习最小树形图,模板题。
Ice_cream’s world II
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
【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】
【Sample Output】
impossible
【分析】
一道裸的最小树形图模板题,算法思路比较简单,但是写起来感觉很不优美...仅作为模板记录吧。
本题不确定起点,故在读入完数据之后建立一个虚根,把所有的点都与这个虚根相连,边权为所有边边权和+1(保证虚边足够长),最后减掉即可。
确定起点的题可以从模板中删掉这部分。
然后就是这个模板是从0开始存储...强迫症好不爽啊...
详细参见:http://www.cnblogs.com/nanke/archive/2012/04/11/2441725.html
/* ***********************************************
MYID : Chen Fan
LANG : G++
PROG : HDU 2121
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int N = ;
const int M = N*N; typedef struct nod
{
int a,b,c;
} node; node edge[M]; int predge[N],id[N],visit[N];//id用来标记圈的
int in[N];//入弧最小的 int ansroot; int dmst(int root,int n,int m)//n表示点数,m表示边数,root表示根
{
int u,v;
int ret=;
while(true)
{
for(int i=;i<n;i++) in[i]=INT_MAX;
for(int i=;i<m;i++)
{
if(edge[i].c<in[edge[i].b]&&edge[i].a!=edge[i].b)
{
predge[edge[i].b]=edge[i].a;//找出每个点的最小入弧
if(edge[i].a==root) ansroot=i;
in[edge[i].b]=edge[i].c;
}
}
for(int i=;i<n;i++)
{
if(i==root) continue;
if(in[i]==INT_MAX) return -;
}
in[root]=;
int cnt=;
memset(id,-,sizeof(id));
memset(visit,-,sizeof(visit));
for(int i=;i<n;i++)
{
ret+=in[i];//进行缩圈
v=i;
while(visit[v]!=i&&id[v]==-&&v!=root)
{
visit[v]=i;
v=predge[v];
}
if(v!=root&&id[v]==-)
{
for(u=predge[v];u!=v;u=predge[u])
id[u]=cnt;
id[v]=cnt++;
}
}
if (cnt==) break;
for (int i=;i<n;i++)
if (id[i]==-) id[i]=cnt++;
for (int i=;i<m;i++)
{
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-=in[v];
}
n=cnt;
root=id[root];
}
return ret;
}
int main()
{
freopen("2121.txt","r",stdin);
int n,m,m1;
while(scanf("%d%d",&n,&m)!=EOF)
{
int a,b;
int r=;
m1=m;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].c);
r+=edge[i].c;
}
r++;
for(int i=;i<n;i++)
{
edge[m].a=n;
edge[m].b=i;
edge[m].c=r;
m++;
}
int ans=dmst(n,n+,m);
ansroot-=m1;//最小根对应的标号为i-m1
if(ans==-||ans>=*r) printf("impossible\n");
else printf("%d %d\n",ans-r,ansroot);
printf("\n");
}
return ;
}