Ice_cream’s world II

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 
 
题目大意:n个城市,m条有向边。问你是否存在一个城市,能通向其他所有城市且权值和最大。如果存在多个这样的城市,那么输出城市编号最小的那个。如果不存在,输出impossible。
 
解题思路:不定根的最小树形图。我们考虑建立一个起点,这个起点跟所有其他的顶点连边,权值为城市中的所有边的权值和+1。然后就是用朱刘算法求解了。
 
 
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<algorithm>
  5. #include<iostream>
  6. #include<vector>
  7. using namespace std;
  8. typedef long long INT;
  9. const int maxn = 1100;
  10. const int INF = 0x3f3f3f3f;
  11. struct Edge{
  12. int from,to;
  13. int dist;
  14. }edges[maxn*maxn];
  15. int pre[maxn],vis[maxn],ID[maxn];
  16. int In[maxn];
  17. int ansidx ;
  18. INT Zhuliu(int root,int n,int m){
  19. INT ret = 0;
  20. int u,v;
  21. while(true){
  22. for(int i = 0; i < n; i++){
  23. In[i] = INF;
  24. }
  25. for(int i = 0; i < m; i++){
  26. Edge &e = edges[i];
  27. u = e.from; v = e.to;
  28. if(In[v] > e.dist && u != v){
  29. pre[v] = u;
  30. if(u == root){ //记录边的编号,这个编号-m就是点编号,因为我们加边是从顶点从小到大
  31. ansidx = i;
  32. }
  33. In[v] = e.dist;
  34. }
  35. }
  36. for(int i = 0; i < n; i++){
  37. if(i == root) continue;
  38. if(In[i] == INF)
  39. return -1;
  40. }
  41. In[root] = 0;
  42. int cntcir = 0;
  43. memset(vis,-1,sizeof(vis));
  44. memset(ID,-1,sizeof(ID));
  45. for(int i = 0; i < n; i++){
  46. ret += In[i];
  47. v = i;
  48. while(vis[v]!= i && ID[v] ==-1 &&v != root){
  49. vis[v] = i;
  50. v = pre[v];
  51. }
  52. if(v != root && ID[v] == -1){
  53. for(u = pre[v]; u != v; u = pre[u]){
  54. ID[u] = cntcir;
  55. }
  56. ID[v] = cntcir++;
  57. }
  58. }
  59. if(cntcir == 0){
  60. break;
  61. }
  62. for(int i = 0; i < n; i++){
  63. if(ID[i]==-1){
  64. ID[i] = cntcir++;
  65. }
  66. }
  67. for(int i = 0; i < m; i++){
  68. v = edges[i].to;
  69. Edge & e = edges[i];
  70. e.from = ID[e.from];
  71. e.to = ID[e.to];
  72. if(e.from != e.to){
  73. e.dist -= In[v];
  74. }
  75. }
  76. n = cntcir;
  77. root = ID[root];
  78. }
  79. return ret;
  80. }
  81. int main(){
  82. int n,m, T, cas = 0;
  83. while(scanf("%d%d",&n,&m)!=EOF){
  84. int a,b,c;
  85. INT sumd = 0;
  86. for(int i = 0; i < m; i++){
  87. scanf("%d%d%d",&a,&b,&c);
  88. a++,b++;
  89. edges[i].from = a;
  90. edges[i].to = b;
  91. if(a == b){
  92. edges[i].dist = INF;
  93. continue;
  94. }
  95. edges[i].dist = c;
  96. sumd += c;
  97. }
  98. for(int i = 0; i < n;i++){
  99. edges[m+i].from = 0;
  100. edges[m+i].to = i + 1;
  101. edges[m+i].dist = sumd + 1;
  102. }
  103. INT res = Zhuliu(0,n+1,m+n);
  104. if(res == -1||res > 2*sumd+1){
  105. puts("impossible");
  106. }else{
  107. printf("%lld %d\n",res - sumd -1,ansidx - m);
  108. }puts("");
  109. }
  110. return 0;
  111. }
  112.  
  113. /*
  114. 3 2
  115. 0 1 2
  116. 1 2 3
  117.  
  118. 3 0
  119.  
  120. */

  

 

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 最小树形图

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

  3. 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 ...

  4. 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 ...

  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 无根最小树形图

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

  7. hdu 2121 Ice_cream’s world II

    Ice_cream’s world II http://acm.hdu.edu.cn/showproblem.php?pid=2121 Time Limit: 3000/1000 MS (Java/O ...

  8. hdu2121 Ice_cream’s world II 最小树形图(难)

    这题比HDU4009要难一些.做了4009,大概知道了最小树形图的解法.拿到这题,最直接的想法是暴力.n个点试过去,每个都拿来做一次根.最后WA了,估计是超时了.(很多题都是TLE说成WA,用了G++ ...

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

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

随机推荐

  1. docker安装oracle

    最近工作上面遇到一个性能相关的问题,大体描述一下: 批量任务执行的过程中导致数据库sql执行时间过长,查看之后是由于批量任务占满数据库连接池导致的,至于为什么批量任务会不受系统控制导致连接池占满就不说 ...

  2. 利用excel制作二维码

    1 将想要通过扫描二维码访问的目标网址放入A1单位格 2 在excel 编辑区右击选择“自定义功能区” 3 然后将“开发者工具”选上 4 点击菜单栏的“开发者工具---插入--->其他控件” 5 ...

  3. 正经学C#_布尔运算[布尔值与其布尔运算符]:《c#入门经典》

    前面几个章节简述了 C#中得常用得算术运算符.这一章节说布尔值与其布尔运算符. 布尔值在c#中表示方式是 bool类型,这个类型可以储存两个值,true或者false,或者真或者假,可以说0或者1. ...

  4. Linux CentOS 7下Memcached 安装与配置

    前言 本篇文章记录一下Linux CentOS 7中关于Memcached的安装与配置. 安装 安装memcached之前首先需要安装libevent,我这里用的版本是: •libevent-2.0. ...

  5. [MOOC程序设计与算法二] 递归二

    1.表达式计算 输入为四则运算表达式,仅由整数.+.-.* ./ .(.) 组成,没有空格,要求求其值.假设运算符结果都是整数 ."/"结果也是整数 表达式也是递归的定义: 表达式 ...

  6. 【转】oracle远程导入数据库

    源地址:http://blog.chinaunix.net/uid-20980141-id-447996.html

  7. loj #547. 「LibreOJ β Round #7」匹配字符串

    #547. 「LibreOJ β Round #7」匹配字符串   题目描述 对于一个 01 串(即由字符 0 和 1 组成的字符串)sss,我们称 sss 合法,当且仅当串 sss 的任意一个长度为 ...

  8. React Native 在用户网络故障时自动调取缓存

    App往往都有缓存功能,例如常见的新闻类应用,如果你关闭网络,你上次打开App加载的数据还在,只是不能加载新的数据了. 我的博客bougieblog.cn,欢迎前来尬聊. 集中处理请求 如果你fetc ...

  9. win10全半角切换

    shift+sapce shift+sapce:全半角切换快捷键,编程的时候发现英文是这种状态,就需要用快捷键切换成半角. (查过老是忘记,在这里写一下记住它)

  10. From表单提交刷新页面?

    form表单提交跳转 写作原因:   楼主的html水平一般,偶然想起周围人常说的form表单提交会刷新页面,闲来无事,就想想其中的原因 想来想去为什么会刷新,猜想了以下几条 1.先提交数据,等服务器 ...