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/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6736 Accepted Submission(s): 1779
3 1
0 1 1
4 4
0 1 10
0 2 10
1 3 20
2 3 30
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
const int INF=0x3f3f3f3f;
#define MAXN 1002
#define MAXM 10005
using namespace std;
struct Edge{
int u,v;
int cost;
}edge[MAXM]; int pre[MAXN],id[MAXN],visit[MAXN];
int in[MAXN];
int minroot; int zhuliu(int root,int n,int m){
int res=;
while(){
for(int i=;i<n;i++){
in[i]=INF;
}
for(int i=;i<m;i++){
if(edge[i].u!=edge[i].v&&edge[i].cost<in[edge[i].v]){
pre[edge[i].v]=edge[i].u;
in[edge[i].v]=edge[i].cost;
if(edge[i].u==root){
minroot=i;
}
}
}
for(int i=;i<n;i++){
if(i!=root && in[i]==INF){
return -;
}
}
int tn=;
memset(id,-,sizeof(id));
memset(visit,-,sizeof(visit));
in[root]=;
for(int i=;i<n;i++){
res+=in[i];
int v=i;
while(visit[v]!=i&&id[v]==-&&v!=root){
visit[v]=i;
v=pre[v];
}
if(v!=root&&id[v]==-){
for(int u=pre[v];u!=v;u=pre[u]){
id[u]=tn;
}
id[v]=tn++;
}
}
if(tn==) break;
for(int i=;i<n;i++){
if(id[i]==-){
id[i]=tn++;
}
}
/* for(int i=0;i<m;){
int v=edge[i].v;
edge[i].u=id[edge[i].u];
edge[i].v=id[edge[i].v];
if(edge[i].u!=edge[i].v){
edge[i++].cost-=in[v];
}
else{
swap(edge[i],edge[--m]);
}
}*/
for(int i=;i<m;i++){
int v=edge[i].v;
edge[i].u=id[edge[i].u];
edge[i].v=id[edge[i].v];
if(edge[i].u!=edge[i].v){
edge[i].cost-=in[v];
}
}
n=tn;
root=id[root];
}
return res;
} int main(){
int n,m;
while(~scanf("%d %d",&n,&m)){
int sum=;
for(int i=;i<m;i++){
scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].cost);
edge[i].u++;
edge[i].v++;
sum+=edge[i].cost;
}
sum++;
for(int i=m;i<n+m;i++){
edge[i].u=,edge[i].v=i-m+,edge[i].cost=sum;
}
int ans=zhuliu(,n+,m+n);
if(ans==-||ans>=*sum){
puts("impossible");
}
else{
printf("%d %d\n",ans-sum,minroot-m);
}
printf("\n");
}
}
Ice_cream’s world II(最小树形图,加虚点)的更多相关文章
- 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 最小树形图
这个题就是需要求整个有向带权图的最小树形图,没有指定根,那就需要加一个虚根 这个虚根到每个点的权值是总权值+1,然后就可以求了,如果求出来的权值大于等于二倍的总权值,就无解 有解的情况,还需要输出最根 ...
- 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 ...
- hdu2121 Ice_cream’s world II 最小树形图(难)
这题比HDU4009要难一些.做了4009,大概知道了最小树形图的解法.拿到这题,最直接的想法是暴力.n个点试过去,每个都拿来做一次根.最后WA了,估计是超时了.(很多题都是TLE说成WA,用了G++ ...
- hdu2121 最小树形图的虚根
/* 最小树形图的第二题,终于有了一些理解 具体看注释 */ /* 无定根的最小树形图 建立虚root 每次只找最短的那条入边 最小树形图理解: 第一步:寻找最短弧集E:扫一遍所有的边,找到每个点权值 ...
- 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 ...
- HDU 2121——Ice_cream’s world II——————【最小树形图、不定根】
Ice_cream’s world II Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- HDU ACM 2121 Ice_cream’s world II (无根最小树形图)
[解题思路]这题先看了NotOnlySuccess的解题思路,即设置虚根再处理的做法:弄了一个上午,再次有种赶脚的感觉~~如果需要找出为什么需要去比所有权值之和更大的数为新增的虚边的话,一开始我理解仅 ...
- hdu 2121 Ice_cream’s world II (无定根最小树形图)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目大意: 有n个点,有m条单向路,问这n个点组成最小树形图的最小花费. 解题思路: 1:构造 ...
随机推荐
- Linux 定制X86平台操作系统
/********************************************************************************* * Linux 定制X86平台操作 ...
- 6-19 Count Connected Components(20 分)
Write a function to count the number of connected components in a given graph. Format of functions: ...
- php 数据类型转换与比较
<?php define("PI", 3.1415926); echo PI."<br>"; //定义一个常量 define("GR ...
- string,char*及CString类型的相互转换
首先先介绍一下什么是CString CString是MFC的字符串类,它不是基本类型,而是对字符串的封装,它是自适应的,在UNICODE环境下就是CStringW,在非UNICODE环境下就是CStr ...
- 【转】Vim自动补全插件----YouCompleteMe安装与配置
原文网址:http://www.cnblogs.com/zhongcq/p/3630047.html 使用Vim编写程序少不了使用自动补全插件,在Linux下有没有类似VS中的Visual Assis ...
- Autocad 2010+ObjectArx 2010 +Vs2010 的.net 开发设置(转)
Autocad 2010+ObjectArx 2010 +Vs2010 的.net 开发设置 分类: ObjectArx.net2010-09-14 16:52 4203人阅读 评论(7) 收藏 举报 ...
- bzoj 4006 [JLOI2015]管道连接——斯坦纳树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4006 除了模板,就是记录 ans[ s ] 表示 s 合法的最小代价.合法即保证 s 里同一 ...
- C#综合揭秘——细说事务
引言 其实事务在数据层.服务层.业务逻辑层多处地方都会使用到,在本篇文章将会为大家一一细说. 其中前面四节是事务的基础,后面的三节是事务的重点,对事务有基础的朋友可以跳过前面四节. 文章有错漏的地方欢 ...
- 02 - Unit010:关联映射
关联映射 什么是? 数据库中有关联关系的表,通过实体对象引用的方式体现出来,叫关联映射. 为什么? 将多表的记录封装成实体对象. 何时用? 对数据库中的表进行多表查询时. 怎么用? cn_user-- ...
- Android开源项目汇总
Android很多优秀的开源项目,很多UI组件以及经典的HTTP 访问开源,都能给我们带来一些自己项目的启迪或者引用. 下面简单介绍一下自己收藏的一些项目内容. 项目: Apollo音乐播放器:And ...