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. “Hello World!”团队第十四次会议

    今天是我们团队“Hello World!”团队召开的第十四次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.Todo List 六.会议照片 七.燃尽图 一.会议时间 2 ...

  2. 感谢——Thunder团队

    团队软件的开发,已经进入第二个阶段——Beta版本了.回头看看,我们走过了很长的一段路,也经历了很多,有意见不一的争吵.有取得暂时成功时的欢欣鼓舞,我们就像一家人,就像那首歌中唱到的,“我们是一家人, ...

  3. HDU 2012 FZU 1756关于素数的一些水题

    HDU 2012 素数判定 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  4. 博弈---巴什博奕(Bash Game)(博弈入门)

    巴什博奕(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规 定每次至少取一个,最多取m个.最后取光者得胜. 显然,如果n=m+1,那么由于一次最多只能取m个,所以,无论先取者拿走 ...

  5. php addslashes和stripslashes函数

    addslashes — 使用反斜线引用字符串 stripslashes — 反引用一个引用字符串   Example #1 一个 addslashes() 例子 <?php$str = &qu ...

  6. lintcode-221-链表求和 II

    221-链表求和 II 假定用一个链表表示两个数,其中每个节点仅包含一个数字.假设这两个数的数字顺序排列,请设计一种方法将两个数相加,并将其结果表现为链表的形式. 样例 给出 6->1-> ...

  7. Maven实现项目构建直接部署Web项目到Tomcat

    Maven实现项目构建直接部署Web项目到Tomcat配置如下: 1.Tomcat的用户及权限配置:在conf目录下,找到tomcat-users.xml,添加manager权限的用户. <ro ...

  8. JAVA第三次笔记

  9. QSet使用及Qt自定义类型使用QHash等算法

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QSet使用及Qt自定义类型使用QHash等算法     本文地址:http://techie ...

  10. 【Mark】Android应用开发SharedPreferences存储数据的使用方法

    Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...