您的位置:首页 > 其它

HDU2121-Ice_cream’s world II(最小树形图)

2017-03-24 22:24 260 查看


Ice_cream’s world II

                                                                                Time Limit: 3000/1000 MS (Java/Others)    Memory
Limit: 32768/32768 K (Java/Others)

                                                                                                            Total Submission(s): 5113    Accepted Submission(s): 1268


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

威士忌

 

题意:n,m分别代表有n个城市,m条边,然后是s,t,c代表一条从s出发到t的边权值是c。求一个城市,这个城市到其他所有城市的总和最小

解题思路:最小树形图,假设一个超级源点0号点,在原有边的基础上,再从0号点到各个点拉一条边,0号点到其他所有点的距离是所有边的总和+1,并且0号点到各个点建边需要其他边建完再建,不然无法找出这个城市,然后以超级源点为起点对新图求一个最小树形图。如果存在两条从超级源点出发的边,那么由于从超级源点出发的边的长度比所有边都要长,而在已经有一条从超级源点出发的边的基础上,第二个点仍然要选择超级源点,那么可以确定,第二个点是单独出来的点,图里存在两个入度为0的点。有超级源点的情况下最小树形图的最大长度(上界)也就是sum+sum-1。因此如果最小树形图的长度>=2*sum,那么就不满足情况了。注意sum不能不加一,我们可以假设存在一种情况有两个点,0条边,那么此时会有两条从超级源点出发的长度为0的边,会认为是满足条件的,实际上0条边是不满足条件的,所以排除掉不成立情况,最后结果就是有超级源点的情况下最小树形图的长度减去sum

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

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;
const int MAXN = 1010;
const int MAXM = 40010;

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

int pre[MAXN],id[MAXN],visit[MAXN],in[MAXN];
int minroot;

int zhuliu(int root,int n,int m,Edge edge[])
{
int res = 0,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].cost < in[edge[i].v])
{
pre[edge[i].v] = edge[i].u;
in[edge[i].v] = edge[i].cost;
if(edge[i].u==root) minroot=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(visit,-1,sizeof(visit));
in[root] = 0;
for(int i = 0; i < n; i++)
{
res += in[i];
v = i;
while( visit[v] != i && id[v] == -1 && v != root)
{
visit[v] = i;
v = pre[v];
}
if( v != root && id[v] == -1 )
{
for(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];
//else swap(edge[i],edge[--m]);
}
n = tn;
root = id[root];
}
return res;
}

int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int sum=0;
int L = 0;
int u,v,w;
for(int i=0; i<m; i++)
{
scanf("%d%d%d",&u,&v,&w);
u++,v++;
edge[L].u = u;
edge[L].v = v;
edge[L++].cost = w;
sum+=w;
}
for(int i=1; i<=n; i++)
{
edge[L].u = 0;
edge[L].v = i;
edge[L++].cost = sum+1;
}
int ans=zhuliu(0,n+1,L,edge);
if(ans==-1||ans>=sum*2+2) printf("impossible\n\n");
else printf("%d %d\n\n",ans-sum-1,minroot-m);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: