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条边的有向 ...
随机推荐
- 本地wamp的Internal Server Error错误解决方法
一.本地wamp下调试url重写,加入htaccess文件后提示:500 Internal Server Error...,而删除这个文件网站又可以正常访问,其实就是没有开启url重写的功能.开启一下 ...
- cocos2d-x一些核心概念截杀
Cocos2d-x中有很多概念,这些概念很多来源于动画.动漫和电影等行业,例如:导演.场景和层等概念,当然也有些有传统的游戏的概念.Cocos2d-x中核心概念:导演, 场景,层,节点,精灵,菜单动作 ...
- mysql插入表中的中文显示为乱码或问号的解决方法
版权声明:本文为博主原创文章,未经博主允许不得转载. 今天在做ssh的博客项目时发现mysql数据库中的中文显示为问号,网上查阅了很多资料,都不是很全,所以我总结一下,供大家参考和自己复习. 1.我的 ...
- 采用Service实现本地推送通知
在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...
- C语言求两个函数中的较大者的MAX函数
//求两个函数中的较大者的MAX函数 #include <stdio.h> int main(int argc, const char * argv[]) { printf("i ...
- sql存在一个表而不在另一个表中的数据
(转)A.B两表,找出ID字段中,存在A表,但是不存在B表的数据.A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引. 方法一 使用 not in ,容易理解,效率低 ...
- shell脚本初识
#!/bin/bash(linux脚本环境的声明即解释器,该解释器为bash,位于根目录下的bin目录下) 变量的定义与赋值: 格式:变量名=变量值(无需声明变量类型) 变量的引用: 格式:$变量名 ...
- WCF 服务的ABC之契约(七)
契约 Contract WCF的所有服务都会公开为契约(Contract),契约与平台无关,是描述服务功能的标注方式. 服务契约(Service Contract) 服务契约描述的是一个服务,它定义了 ...
- JS判断鼠标从什么方向进入一个容器
偶然将想到的一个如何判断鼠标从哪个方向进入一个容器的问题.首先想到的是给容器的四个边添加几个块,然后看鼠标进入的时候哪个块先监听到鼠标事件.不过这样麻烦太多了.google了一下找到了一个不错的解决方 ...
- python: 实现sha1小工具
File1: sha1.py File2: sha1.bat ------------------ File1: sha1.py import hashlib import os,sys def Ca ...