Ice_cream’s world II

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6849    Accepted Submission(s): 1818

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121

Description:

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.

Input:

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.

Output:

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.

Sample Input:

3 1
0 1 1 4 4
0 1 10
0 2 10
1 3 20
2 3 30

Sample Output:

impossible

40 0

题意:

给出一个有向图,然后让你选一个点作为起点,满足从这个点可以到达其它点,并且总权和最小。

题解:

这就是一个不固定根的有向图最小生成树问题。思路还是挺有趣的。

我们不可能对每个点都跑一遍朱刘算法,所以考虑加一个虚根,然后边权为INF,直接从这个虚根来跑就行了。最后答案就是跑出来的值减去INF。

但是这里要注意的是一些不合法的情况,就是最后的答案大于2*INF时,因为这时,说明至少有两个点入度为0,那么说明不可能存在一颗有向图的生成树。

至于这个INF怎么取,只要满足大于等于边权和就行了(我是这么想的),不知道为什么不能等于,这个问题我纠结了很久,就在刚才知道为什么了。。

可能存在只有一条边的情况,这时如果连边权和,这时算法可能会出错。

最后输出起点,这里比较巧妙,是根据边来的,因为我们会改变点(算法缩点),但边并没有改变,具体见代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
#define INF 1e17
using namespace std;
typedef long long ll;
int n,m,t;
const int N = ,M = ;
struct Edge{
int u,v,w;
}e[M<<];
int pre[N]; //记录前驱
int Rt;
ll id[N],vis[N],in[N];
ll dirMst(int root){
ll ans=;
while(){
for(int i=;i<=n;i++) in[i]=INF;
memset(id,-,sizeof(id));
memset(vis,-,sizeof(vis));
for(int i=;i<=m;i++){
int u=e[i].u,v=e[i].v,w=e[i].w;
if(w<in[v] && v!=u){
pre[v]=u;
in[v]=w;
if(u==root) Rt=i;
}
} //求最小入边集
in[root]=;
pre[root]=root;
for(int i=;i<n;i++){
if(in[i]==INF) return -;
ans+=in[i];
}
int idx = ; //新标号
for(int i=;i<n;i++){
if(vis[i] == - ){
int u = i;
while(vis[u] == -){
vis[u] = i;
u = pre[u];
}
if(vis[u]!=i || u==root) continue; //判断是否形成环
for(int v=pre[u];v!=u;v=pre[v] )
id[v]=idx;
id[u] = idx++;
}
}
if(idx==) break;
for(int i=;i<n;i++){
if(id[i]==-) id[i]=idx++;
}
for(int i=;i<=m;i++){
e[i].w-=in[e[i].v];
e[i].u=id[e[i].u];
e[i].v=id[e[i].v];
}
n = idx;
root = id[root];//给根新的标号
}
return ans;
} int main(){
while(scanf("%d%d",&n,&m)!=EOF){
ll sum = ;
for(int i=;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
e[i]=Edge{u,v,w};
sum+=w;
}
sum++;
for(int i=;i<n;i++){
e[i+m+]=Edge{n,i,sum};
}
ll tmp=m;
m+=n;n++;
ll ans = dirMst(n-);
if(ans>*sum) printf("impossible\n\n");
else printf("%lld %lld\n\n",ans-sum,Rt-tmp-1ll);
}
}

HDU2121:Ice_cream’s world II (虚根+有向图最小生成树)的更多相关文章

  1. hdu2121 Ice_cream's world II

    hdu2121 Ice_cream's world II 给一个有向图,求最小树形图,并输出根节点 \(n\leq10^3,\ m\leq10^4\) 最小树形图 对于求无根最小树形图,可以建一个虚拟 ...

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

  3. hdu2121 - Ice_cream’s world II(朱刘算法,不固定根)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目意思大概是要你在一些城市中选一个做首都 , 要求首都都能到其他城市 , 道路花费要最少 , ...

  4. hdu2121 Ice_cream’s world II 最小树形图(难)

    这题比HDU4009要难一些.做了4009,大概知道了最小树形图的解法.拿到这题,最直接的想法是暴力.n个点试过去,每个都拿来做一次根.最后WA了,估计是超时了.(很多题都是TLE说成WA,用了G++ ...

  5. HDU2121 Ice_cream’s world II (最小树形图)

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

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

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

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

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

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

  9. Uva11183-Teen Girl Squad(有向图最小生成树朱刘算法)

    解析: 裸的有向图最小生成树 代码 #include<cstdio> #include<cstring> #include<string> #include< ...

随机推荐

  1. Multi-task Correlation Particle Filter for Robust Object Tracking--论文随笔

    摘要:在这篇论文中,作者提出一种鲁棒视觉跟踪的多任务相关粒子滤波琪跟踪算法(MCPF).作者首先向我们展示了多任务相关滤波器,该滤波器在训练滤波器模板的时候可以学习不同特征之间的联系.本文提出的MCP ...

  2. python函数学习之装饰器

    装饰器 装饰器的本质是一个python函数,它的作用是在不对原函数做任何修改的同时,给函数添加一定的功能.装饰器的返回值也是一个函数对象. 分类: 1.不带参数的装饰器函数: def wrapper( ...

  3. Chameleon-mini简介

    ChameleonMini(变色龙)原德国大学在研究RFID安全时所设计的一块针对多频段多类型RFID模拟的硬件,其设计本身支持ISO14443和ISO15693标准协议,最简单直接的用法就是把获取到 ...

  4. MongoDB3.2 集群搭建

    一.集群的理论知识 1.1 集群成员 MongoDB的集群类似于GreenPlum集群,由一个入口节点负责任务分发与结果统计,分片结节负责执行任务.不同GP,多了一个config servers. 集 ...

  5. 新人学PHP,认为手动搭建环境而苦恼吗?这篇文章告诉你多简单!

    本教程适用于初学PHP,想了解手动搭建PHP环境的童鞋. 一键环境和高手勿喷. 本教程以下列版本软件为例: 所需软件目录 我在这里的目录结构是(个人习惯) 安装与配置 apache 双击安装Apach ...

  6. postmortem report of period M2

    一.设想和目标 1.我们的软件主要要解决学长设计的学霸系统中视频及文档的浏览功能问题. 2.时间相对充裕.不过对于我们这些零基础的人来说还是比较困难. 3.我们团队中不同意见通常会进行进一步讨论,说出 ...

  7. Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片

    一. Application用途 1. Application用途 创建Application时机 : Application在启动的时候会调用Application无参的构造方法创建实例; Appl ...

  8. css那些事儿3 列表与浮动

    一  列表 列表默认为行内块元素,具有宽高,当一个非块元素是无法应用宽高的,比如a 1 有序列表 有ol li组成,其中li为列表项,列表的ol子元素务必为li元素标签,li子内容支持列表任意嵌套,有 ...

  9. phpcms开启在线编辑模版 方法

    目录:\caches\configs\system.php 将:第20行 'tpl_edit'=> 0   修改为  'tpl_edit'=> 1   (0:默认的,不开启:     1: ...

  10. G# GUID

    GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.通常平台会提供生成GUID的API.生成算法很有意思,用到了以太网卡地址.纳秒级时间.芯片ID码和许多可 ...