HDU 2121 Ice_cream’s world II 不定根最小树形图
题目链接:
题目
Ice_cream’s world II
Time Limit: 3000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
问题描述
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.
输入
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.
输出
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.
样例
input
3 1
0 1 1
4 4
0 1 10
0 2 10
1 3 20
2 3 30
output
impossible
40 0
题意
不固定根最小树形图,并且多解时,输出根编号最小的解
题解
加一个虚根,与所有的点连接单向边,权值都为"所有边权和+1"。加这额外的n条边时,按顶点编号从小到大加。然后跑朱刘算法。
(为什么要所有权值+1呢?因为最后如果答案>=2*(所有边权和+1),就说明额外加的边用了不只一条,也就是根本无解的!)
代码
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 1111;
const int maxm = 20101;
const int INF = 0x7fffffff;
struct Edge {
int u, v, w;
Edge(int u,int v,int w):u(u),v(v),w(w){}
Edge() {}
}egs[maxm];
int N, M;
int pos;
int in[maxn],id[maxn], vis[maxn], pre[maxn];
int Directed_MST(int rt,int n,int m) {
int ret = 0;
while (1) {
//求最小入度边
for (int i = 0; i < n; i++) in[i] = INF;
for (int i = 0; i < m; i++) {
Edge& e = egs[i];
if (e.w < in[e.v] && e.u != e.v) {
//如果存在多解的话,那么那几个解一定在一个环上,因为以任意一个为根,都能到达其他点且使总权值最小。
//既然在环上最后就一定会缩成同一个点,虚根与它们的边就变成平行边了!由于我们额外的边是按节点编号从大到小排的,那么我们就只会记录编号最小的那个点了。
//这里不能记录pos=e.v,因为缩点之后e.v改变了
if (e.u == rt) pos = i; //这里rt就是须根了!!!缩点之后不会是N的!!!
in[e.v] = e.w;
pre[e.v] = e.u;
}
}
for (int i = 0; i < n; i++) {
if (i!=rt&&in[i] == INF) return -1;
}
int tot = 0;
memset(id, -1, sizeof(id));
memset(vis, -1, sizeof(vis));
in[rt] = 0;
//找环,缩点
for (int i = 0; i < n; i++) {
ret += in[i];
int v = i;
while (vis[v] != i&&id[v] == -1 && v != rt) {
vis[v] = i;
v = pre[v];
}
if (id[v] == -1 && v != rt) {
for (int u = pre[v]; u != v; u = pre[u]) {
id[u] = tot;
}
id[v] = tot++;
}
}
//没有环
if (tot == 0) break;
for (int i = 0; i < n; i++) {
if (id[i] == -1) id[i] = tot++;
}
//更新到环的距离
for (int i = 0; i < m; i++) {
Edge& e = egs[i];
int v = e.v;//这个v要留下来!
e.u = id[e.u],e.v = id[e.v];
if (e.u != e.v) {
e.w -= in[v];
}
}
n = tot;
rt = id[rt];
}
return ret;
}
int main() {
while (scanf("%d%d", &N, &M) == 2 && N) {
int sum = 0;
for (int i = 0; i < M; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
sum += w;
egs[i] = Edge(u, v, w);
}
sum++;
for (int i = 0; i < N; i++) {
egs[M + i] = Edge(N, i, sum);
}
int ans = Directed_MST(N, N + 1, N+M);
if (ans == -1 || ans >= sum * 2) {
puts("impossible");
}
else {
printf("%d %d\n", ans - sum, pos - M);
}
puts("");
}
return 0;
}
HDU 2121 Ice_cream’s world II 不定根最小树形图的更多相关文章
- HDU 2121——Ice_cream’s world II——————【最小树形图、不定根】
Ice_cream’s world II Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- HDU 2121 Ice_cream’s world II 最小树形图 模板
开始学习最小树形图,模板题. Ice_cream’s world II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32 ...
- hdu 2121 Ice_cream’s world II (无定根最小树形图)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目大意: 有n个点,有m条单向路,问这n个点组成最小树形图的最小花费. 解题思路: 1:构造 ...
- HDU - 2121 Ice_cream’s world II 无根最小树形图
HDU - 2121 :http://acm.hdu.edu.cn/showproblem.php?pid=2121 比较好的朱刘算法blog:https://blog.csdn.net/txl199 ...
- HDU 2121 Ice_cream’s world II 最小树形图
这个题就是需要求整个有向带权图的最小树形图,没有指定根,那就需要加一个虚根 这个虚根到每个点的权值是总权值+1,然后就可以求了,如果求出来的权值大于等于二倍的总权值,就无解 有解的情况,还需要输出最根 ...
- 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 ...
- 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 ...
- HDU2121 Ice_cream’s world II (最小树形图)
在建图的时候对原图进行加边 建立一个超级源点~ #include<cstdio> #include<algorithm> #include<cstring> usi ...
- hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】
称号:pid=2121" target="_blank">hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向 ...
随机推荐
- slickgrid 一个优秀的JS表格插件
从熟悉JS以来,慢慢的喜欢上了这个门语言. 不自觉的,看了好多js的知识,可能也是因为做项目的原因吧. 这里稍微说下一个JS的grid插件 --slickgrid 了解这个插件也不是很多,稍微了解了下 ...
- 安装ie10慢的解决办法
下面是win7安装ie10的先决条件: http://support.microsoft.com/kb/2818833
- DEEPIN下搭建FTP服务器步骤(备忘录)
1.打开终端,执行命令[apt-get install vsftpd],安装VSFTPD 2.安装完成后,修改以下配置信息(否则文件无法传输) [echo 'listen=YES'>>/e ...
- 转: 理解Python的With语句
Python’s with statement provides a very convenient way of dealing with the situation where you have ...
- 关联表映射 Association Table Mapping
把关联保存为一个表,存储关联表的外键 在对象中,使用集合作为域值,来处理多值域. 而在DB中,只能有单值域. 外键映射的核心,是在关联关系的单值端使用外键来维持联系. 而在多对多的关联关系中,已经不存 ...
- 9个超绚丽的HTML5 3D图片动画特效
在Web 1.0时代,我们的网页中图片数量非常少,而且都是以静态图片为主.HTML5的出现,推动了Web 2.0的发展,同时也催生出了很多绚丽的HTML5图片动画特效,特别是有些还有3D的动画效果.本 ...
- 10款很好用的 jQuery 图片滚动插件
jQuery 作为最流行的 JavaScript 框架,使用简单灵活,同时还有许多优秀的插件可供使用.其中最令人印象深刻的应用之一就是各种很酷的图片效果,它可以让的网站更具吸引力.这里收集了10款很好 ...
- 返璞归真vc++之字符类型
在今天,大量使用java与.net的程序员已经很少去真实了解字符的底层表达,但是使用VC++编程,对字符的处理却非常慎重,刚学习vc++肯定会为其中的字符类型给晕头转向,今天本人学习第一节,从字符开始 ...
- 【风马一族_Android】多选按钮的监控事件
import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android ...
- 《通过脚本查看哪些ip被占用》shell笔记
改脚本查看哪些ip被占用. #!/bin/bash for i in {1..10} //赋予i变量1-10 do //干什么 ping -c1 -w1 192.168.7.$i && ...