题目链接

题意

求有向图的最小生成树,且根不定。

思路

最小树形图即求有向图的最小生成树,用的是朱刘算法。

这里不定根,那么可以建立一个虚根,让虚根和所有点相连,权值为一个很大的数(这里直接设为所有边之和+1)。

如果最后的答案比两倍的sum还大,就说明至少有两个点是通过虚边(从虚点走出去的边)相连(因为虚边的边权很大),那么这也是一个不连通的图。

找真正的根的话,只要找和虚根相连并且走过虚边的点就是了。

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 11;
const int INF = 0x3f3f3f3f;
typedef long long LL;
struct Edge {
int u, v; LL w;
} edge[N*2];
int tot, n, m, minroot;
int pre[N], id[N], vis[N];
LL in[N]; void Add(int u, int v, LL w) {
edge[tot++] = (Edge) { u, v, w };
} LL zhuliu(int root, int n, int m) {
LL ans = 0;
int u, v; LL w;
while(true) {
for(int i = 0; i < n; i++) in[i] = 1e17; for(int i = 0; i < m; i++) { // 找最小入边
u = edge[i].u, v = edge[i].v, w = edge[i].w;
if(u != v && w < in[v]) pre[v] = u, in[v] = w,
minroot = (u == root ? i : minroot); // 只有这里找根和模板不一样
} for(int i = 0; i < n; i++) // 存在孤立点
if(i != root && in[i] == 1e17) return -1; int tn = 0;
memset(id, -1, sizeof(id));
memset(vis, -1, sizeof(vis));
in[root] = 0; for(int i = 0; i < n; i++) { // 找环
ans += in[i];
v = i;
while(vis[v] != i && id[v] == -1 && v != root)
vis[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++) { // 更新其他点到环的距离
u = edge[i].u, v = edge[i].v;
edge[i].u = id[u];
edge[i].v = id[v];
if(edge[i].u != edge[i].v)
edge[i].w -= in[v];
}
n = tn;
root = id[root];
}
return ans;
} int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(~scanf("%d%d", &n, &m)) {
tot = 0; LL sum = 0;
for(int i = 0; i < m; i++) {
int u, v; LL w;
scanf("%d%d%lld", &u, &v, &w);
Add(u, v, w); sum += w;
}
sum++;
for(int i = 0; i < n; i++)
Add(n, i, sum);
LL ans = zhuliu(n, n + 1, tot); // 虚根为n
// printf("ans : %lld\n", ans);
if(ans == -1 || ans >= 2 * sum) puts("impossible");
else printf("%lld %d\n", ans - sum, minroot - m);
puts("");
}
return 0;
}

HDU 2121:Ice_cream’s world II(不定根的最小树形图)的更多相关文章

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

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

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

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

  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——————【最小树形图、不定根】

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 如何自定义WPF项目的Main函数

    原文:如何自定义WPF项目的Main函数 与Winform项目不同,WPF项目的Main函数在项目生成的时候,系统自动在后台为我们生成.根据项目生成方式的不同,其文件位于obj/Debug/App.g ...

  2. ASP .NET Model

    Model是全局变量,一个页面一个 前台 @ModelWebApplication1.Models.Movie; @{ ViewBag.Title = "ModelTest"; } ...

  3. C# System.Threading.Timer的使用

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  4. delphi的拖拽功能实现

    惭愧,编了这么多年程序,还没用过拖拽功能 这次同事要实现图标互换的功能,让我帮忙看一下,于是趁机研究了一下拖拽事件,发现还是比较简单的 参考了http://topic.csdn.net/u/20081 ...

  5. Aspect Oriented Programming面向切面编程

    I简介 Aspect Oriented Programming(AOP),面向切面编程,是一个比较热门的话题.AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或 ...

  6. Wpf发送接收 win32消息

    #region WPF发送和接收win32消息 public const int WM_GETTEXT = 0x0D; public const int WM_SETTEXT = 0x0C; publ ...

  7. DateTime格式转换结果

    Console.WriteLine(string.Format("ToLongDateString:{0}", DateTime.Now.ToLongDateString())); ...

  8. Windows 10开发基础——启动默认应用的URI

    主要内容:通过指定的URI来启动默认的应用(设置,应用商店,地图,人脉) 方法一:直接在XAML中添加如下代码 <TextBlock x:Name="LocationDisabledM ...

  9. 【Linux】简单明了查看内存使用和ubuntu的版本号及位数

    1.查看ubuntu的版本号:cat /etc/issue 2.查看系统是32位的还是64位:getconf LONG_BIT 3.查看内存使用 free free命令可以用来查看系统内存使用情况,- ...

  10. C#获取字符串宽度像素

    通过Graphics对象的MeasureString方法可以获取字符串的大小,如下: Graphics graphics = CreateGraphics(); SizeF sizeF = graph ...