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)

Problem 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
 
Author
Wiskey
 
Source
 
 
威士忌   |   We have carefully selected several similar
problems for you:  2120 2122 1222 4009 2064 
 
 
没有根的最小树形图
新建虚拟节点,向所有点连权值为 边权和+1的边
最后再减去这条边
 
记录根节点时,记录虚拟边的编号,不要记录虚拟边指向的点,因为缩环之后点的编号有所改变
 
#include<cstdio>
#include<cstring>
#define N 1005
#define M 10005
#define inf 2e9
using namespace std;
struct node
{
int u,v,w;
}e[M+N];
int in[N],pre[N],vis[N],col[N],id[N];
int ROOT;
int n,m;
int directed_MST()
{
int tot=n+,root=,ans=,cirnum=,to;
while()
{
for(int i=;i<tot;i++) in[i]=inf;
for(int i=;i<=m;i++)
if(e[i].u!=e[i].v && in[e[i].v]>e[i].w)
{
in[e[i].v]=e[i].w;
pre[e[i].v]=e[i].u;
if(e[i].u==root) ROOT=i;
}
cirnum=;
memset(vis,-,sizeof(vis));
memset(col,-,sizeof(col));
in[root]=;
for(int i=;i<tot;i++)
{
ans+=in[i];
to=i;
while(vis[to]!=i && col[to]==- && to!=root)
{
vis[to]=i;
to=pre[to];
}
if(to!=root && col[to]==-)
{
for(int nt=pre[to];nt!=to;nt=pre[nt])
col[nt]=cirnum;
col[to]=cirnum++;
}
}
if(!cirnum) return ans;
for(int i=;i<tot;i++)
if(col[i]==-) col[i]=cirnum++;
for(int i=;i<=m;i++)
{
to=e[i].v;
e[i].u=col[e[i].u];
e[i].v=col[e[i].v];
if(e[i].u!=e[i].v) e[i].w-=in[to];
}
tot=cirnum;
root=col[root];
}
return ans;
}
int main()
{
int tot,sum;
int u,v,w,ans,tmp;
while(scanf("%d%d",&n,&m)!=EOF)
{
tot=sum=;
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
if(u!=v)
{
u++; v++;
e[++tot].u=u; e[tot].v=v; e[tot].w=w;
sum+=w;
}
}
tmp=tot;
for(int i=;i<=n;i++)
{
e[++tot].u=; e[tot].v=i; e[tot].w=sum+;
}
m=tot;
ans=directed_MST();
if(ans>=*(sum+)) printf("impossible\n\n");
else printf("%d %d\n\n",ans-(sum+),ROOT-tmp-);
}
}

hdu 2121 Ice_cream’s world II的更多相关文章

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

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

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

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

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

  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 (无定根最小树形图)

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

  6. HDU 2121 Ice_cream’s world II 最小树形图

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

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

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

  8. HDU ACM 2121 Ice_cream’s world II (无根最小树形图)

    [解题思路]这题先看了NotOnlySuccess的解题思路,即设置虚根再处理的做法:弄了一个上午,再次有种赶脚的感觉~~如果需要找出为什么需要去比所有权值之和更大的数为新增的虚边的话,一开始我理解仅 ...

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

随机推荐

  1. struts-resultType属性

    1.默认dispatcher:forward方式,服务器端跳转 2.redirect:客户端跳转 3.chain:Action转发,forward方式,服务器端跳转action 4.redirectA ...

  2. SVN版本合并技巧

    公司使用了bug管理系统,项目添加新功能时,建一个主工单,再分成多个子工单,将子工单分给多个程序员来开发. 开发人员完成一部分就提交一部分,多个小功能模块就分多次提交到测试主干,然后用测试主干项目发布 ...

  3. 第九章 Mysql函数

    简介 数学函数:处理数字 字符串函数:处理字符串 日期和时间函数:处理日期和时间,获取时间 条件判断函数:控制条件选择 系统信息函数:获取MySQL系统信息,包括数据库名称,当前用户名和数据库版本 加 ...

  4. View 渲染

    在Spring MVC 中,controllers不负责具体的页面渲染,仅仅是调用业务逻辑并返回model数据给view层,至于view层具体怎么展现,由专门的view层具体负责,这就是MVC模式,业 ...

  5. request内置对象在JSP

  6. TeamCity编译执行selenium上传窗口脚本缺陷

    2015-07-04 18:05 编写本文 TeamCity编译selenium脚本,对于上传窗口处理只支持sendKeys的使用,不支持模拟人为按下Enter键和使用autoIt等操作,即使本地调试 ...

  7. Django内置的分页模块

    自定义分页 未封装版: 优点:直观 缺点:代码乱,不易维护,可拓展性差 data = [] for i in range(1, 302): tmp = {"id": i, &quo ...

  8. javascript之容易出错的地方

    1: 不是所有的非空对象都有toString()方法的 var obj = Object.create(null); console.log(obj.toString());   // false; ...

  9. js & right click menu & 鼠标滑词

    js & right click menu & 鼠标滑词 // 鼠标滑词 mouseSlipGetWords() { const getSelectionText = () => ...

  10. hdu mophues

    在比赛的时候,被这个题目虐死了,这一周中每当我有空闲时间我就总是思索这个题目的解题方法. 终于在自己学过了mobius反演,并且看过别人写得解题思路后自己有了思路. 下面说说我的解题思路吧. 首先题目 ...