Ice_cream’s world II

http://acm.hdu.edu.cn/showproblem.php?pid=2121

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
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
 
 
威士忌   |   We have carefully selected several similar
problems for you:  2120 2122 1222 4009 2064 
 
 
没有根的最小树形图
新建虚拟节点,向所有点连权值为 边权和+1的边
最后再减去这条边
 
记录根节点时,记录虚拟边的编号,不要记录虚拟边指向的点,因为缩环之后点的编号有所改变
 
#include<cstdio>
#include<cstring>
#define N 1005
#define M 10005
#define inf 2e9
using namespace std;
struct node
{
int u,v,w;
}e[M+N];
int in[N],pre[N],vis[N],col[N],id[N];
int ROOT;
int n,m;
int directed_MST()
{
int tot=n+,root=,ans=,cirnum=,to;
while()
{
for(int i=;i<tot;i++) in[i]=inf;
for(int i=;i<=m;i++)
if(e[i].u!=e[i].v && in[e[i].v]>e[i].w)
{
in[e[i].v]=e[i].w;
pre[e[i].v]=e[i].u;
if(e[i].u==root) ROOT=i;
}
cirnum=;
memset(vis,-,sizeof(vis));
memset(col,-,sizeof(col));
in[root]=;
for(int i=;i<tot;i++)
{
ans+=in[i];
to=i;
while(vis[to]!=i && col[to]==- && to!=root)
{
vis[to]=i;
to=pre[to];
}
if(to!=root && col[to]==-)
{
for(int nt=pre[to];nt!=to;nt=pre[nt])
col[nt]=cirnum;
col[to]=cirnum++;
}
}
if(!cirnum) return ans;
for(int i=;i<tot;i++)
if(col[i]==-) col[i]=cirnum++;
for(int i=;i<=m;i++)
{
to=e[i].v;
e[i].u=col[e[i].u];
e[i].v=col[e[i].v];
if(e[i].u!=e[i].v) e[i].w-=in[to];
}
tot=cirnum;
root=col[root];
}
return ans;
}
int main()
{
int tot,sum;
int u,v,w,ans,tmp;
while(scanf("%d%d",&n,&m)!=EOF)
{
tot=sum=;
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
if(u!=v)
{
u++; v++;
e[++tot].u=u; e[tot].v=v; e[tot].w=w;
sum+=w;
}
}
tmp=tot;
for(int i=;i<=n;i++)
{
e[++tot].u=; e[tot].v=i; e[tot].w=sum+;
}
m=tot;
ans=directed_MST();
if(ans>=*(sum+)) printf("impossible\n\n");
else printf("%d %d\n\n",ans-(sum+),ROOT-tmp-);
}
}

hdu 2121 Ice_cream’s world II的更多相关文章

  1. HDU 2121 Ice_cream’s world II 最小树形图 模板

    开始学习最小树形图,模板题. Ice_cream’s world II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32 ...

  2. HDU 2121——Ice_cream’s world II——————【最小树形图、不定根】

    Ice_cream’s world II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  3. HDU 2121 Ice_cream’s world II 不定根最小树形图

    题目链接: 题目 Ice_cream's world II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  4. HDU - 2121 Ice_cream’s world II 无根最小树形图

    HDU - 2121 :http://acm.hdu.edu.cn/showproblem.php?pid=2121 比较好的朱刘算法blog:https://blog.csdn.net/txl199 ...

  5. hdu 2121 Ice_cream’s world II (无定根最小树形图)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目大意: 有n个点,有m条单向路,问这n个点组成最小树形图的最小花费. 解题思路: 1:构造 ...

  6. HDU 2121 Ice_cream’s world II 最小树形图

    这个题就是需要求整个有向带权图的最小树形图,没有指定根,那就需要加一个虚根 这个虚根到每个点的权值是总权值+1,然后就可以求了,如果求出来的权值大于等于二倍的总权值,就无解 有解的情况,还需要输出最根 ...

  7. hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】

    称号:pid=2121" target="_blank">hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向 ...

  8. HDU ACM 2121 Ice_cream’s world II (无根最小树形图)

    [解题思路]这题先看了NotOnlySuccess的解题思路,即设置虚根再处理的做法:弄了一个上午,再次有种赶脚的感觉~~如果需要找出为什么需要去比所有权值之和更大的数为新增的虚边的话,一开始我理解仅 ...

  9. HDU2121 Ice_cream’s world II —— 最小树形图 + 不定根 + 超级点

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 Ice_cream’s world II Time Limit: 3000/1000 MS (J ...

随机推荐

  1. PSP Daily软件Alpha版本——基于spec评论

    题目要求:每个小组评论其他小组Alpha发布作品的软件功能说明书.要求和提交在[https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/122 ...

  2. Android 6.0 中的 Wifi 连接

    Android 6.0 中的 Wifi 连接 这几天在写一个软件,结果被其中的 wifi 连接问题困扰了 3 天. 先描述下需求: usb 接口接了一根 usb2serial,通过这个接口接收命令 当 ...

  3. mac安装php分词工具xunsearch出现找不到bio.h的解决办法

    下载xunsearch后安装出现如下错误,在xunsearch官方论坛未找到答案,此方案不仅用于参考解决安装xunsearch,其它编辑安装出现的问题同样可以参考 -n Checking scws . ...

  4. 博弈---威佐夫博奕(Wythoff Game)

    这个写的不错 威佐夫博奕(Wythoff Game):有两堆各若干个物品,两个人轮流从某一堆或同 时从两堆中取同样多的物品,规定每次至少取一个,多者不限,最后取光者得胜.     这种情况下是颇为复杂 ...

  5. Codeforces Round #335 (Div. 2) D. Lazy Student 贪心+构造

    题目链接: http://codeforces.com/contest/606/problem/D D. Lazy Student time limit per test2 secondsmemory ...

  6. DescriptionAttribute Class

    指定属性或事件的描述. [Description("The image associated with the control"),Category("Appearanc ...

  7. HTML5 <meta> 标签属性,所有meta用法

    基本标签 声明文档使用的字符编码:<meta charset="utf-8" /> 声明文档的兼容模式:<meta http-equiv="X-UA-C ...

  8. c文法

    程序→<外部声明>|<程序> 外部声明→<功能定义>|<声明> 功能定义→<声明复合语句的类型> 类型→<VOID| CHAR| IN ...

  9. CCF——图像旋转201503-1

    问题描述 旋转是图像处理的基本操作,在这个问题中,你需要将一个图像逆时针旋转90度. 计算机中的图像表示可以用一个矩阵来表示,为了旋转一个图像,只需要将对应的矩阵旋转即可. 输入格式 输入的第一行包含 ...

  10. 奇异值分解(SVD)原理详解及推导(转载)

    转载请声明出处http://blog.csdn.net/zhongkejingwang/article/details/43053513 在网上看到有很多文章介绍SVD的,讲的也都不错,但是感觉还是有 ...