题目链接:

题目

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 不定根最小树形图的更多相关文章

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

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

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

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

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

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

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

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

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

    在建图的时候对原图进行加边 建立一个超级源点~ #include<cstdio> #include<algorithm> #include<cstring> usi ...

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

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

随机推荐

  1. select--from--where--group by--having--order by 依次顺序

    1.查询中用到的关键词主要包含六个,并且他们的顺序依次为 select--from--where--group by--having--order by 其中select和from是必须的,其他关键词 ...

  2. iOS-NSOperation多线程

    NSOperation 一.简介 1.使用步骤 配合使用NSOperation和NSOperationQueue也能实现多线程编程 先将操作封装到一个NSOperation对象中 然后将NSOpera ...

  3. UI4_UITableViewSectionIndex

    // AppDelegate.m // UI4_UITableViewSectionIndex // // Created by zhangxueming on 15/7/14. // Copyrig ...

  4. javascript弹窗基础篇

    confirm()意既确认框 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  5. C++ Sets

    集合(Set)是一种包含已排序对象的关联容器 begin() 返回指向第一个元素的迭代器 clear() 清除所有元素 count() 返回某个值元素的个数 empty() 如果集合为空,返回true ...

  6. 如何解决win7系统访问共享服务器慢的问题

    最近重新装了win7系统,但发现当访问共享服务器的时候很慢,后来在网上搜索了相关解决办法,在此和大家分享下: 1. 通过“windows键”+“R键”快捷打开“运行”窗口,然后输入cmd命令敲回车,进 ...

  7. android-support-xxxx.jar NoClassDefFoundError

    当你的项目出现以下红色提示的时候,要小心了,因为很可能因为这个错误而导致解释不通的异常出现. Found 2 versions of android-support-v4.jar in the dep ...

  8. 腾讯微博OAuth2.0 .NET4.0 SDK 发布以及网站腾讯微博登陆示例代码(原创)

    1.使用简单方便,包含详细注释: 2.暂时只支持xml格式字符串的转换,建议接口使用xml参数:3.QweiboSDK.Controllers命名空间下已包含所有API接口:4.只需调用到Qweibo ...

  9. hadoop架构

    HADOOP中可以分为两个大的模块,存储模块和计算模块.HDFS作为存储模块,JobTracker,TaskTracker构成计算模块.   1.HADOOP的文件是以HDFS格式存储的   HDFS ...

  10. 2013-07-22 IT 要闻速记快想

    ### ========================= ### 如何让用户点击广告.观看广告并乐在其中?这个问题的答案精彩纷呈.有的公司开创模式,为点击广告的用户提供优惠券:有的公司想法新奇,让用 ...