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/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
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 “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.
#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的更多相关文章
- 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——————【最小树形图、不定根】
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/32768 K (Ja ...
- 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 (无定根最小树形图)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目大意: 有n个点,有m条单向路,问这n个点组成最小树形图的最小花费. 解题思路: 1:构造 ...
- HDU 2121 Ice_cream’s world II 最小树形图
这个题就是需要求整个有向带权图的最小树形图,没有指定根,那就需要加一个虚根 这个虚根到每个点的权值是总权值+1,然后就可以求了,如果求出来的权值大于等于二倍的总权值,就无解 有解的情况,还需要输出最根 ...
- hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】
称号:pid=2121" target="_blank">hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向 ...
- HDU ACM 2121 Ice_cream’s world II (无根最小树形图)
[解题思路]这题先看了NotOnlySuccess的解题思路,即设置虚根再处理的做法:弄了一个上午,再次有种赶脚的感觉~~如果需要找出为什么需要去比所有权值之和更大的数为新增的虚边的话,一开始我理解仅 ...
- 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 ...
随机推荐
- VMware提示无法打开内核设备 \\.\Global\vmx86: 系统找不到指定的文件解决方案
1.右键单击[我的电脑],选择[管理] 2.在[服务]中找到VMware Workstation Server服务右键启动
- 关于算法的时间复杂度O(f(n))
(一)算法时间复杂度定义: 在进行算法分析时,语句总的执行次数T(n)是关于问题规模n的函数,进而分析T(n)随n的变化情况并确定T(n)的数量级.算法的时间复杂度,也就是算法的时间量度,记作:T(n ...
- 《JavaScript》JS中的跨域问题
参考博客:https://www.cnblogs.com/yongshaoye/p/7423881.html
- Java微笔记(8)
Java 中的包装类 Java 为每个基本数据类型都提供了一个包装类,这样就可以像操作对象那样来操作基本数据类型 基本类型和包装类之间的对应关系: 包装类主要提供了两大类方法: 将本类型和其他基本类型 ...
- 设计 Azure SQL 数据库,并使用 C# 和 ADO.NET 进行连接
标题:设计 Azure SQL 数据库,并使用 C# 和 ADO.NET 进行连接 里面有使用C#使用SqlServer的例子.
- JAVA方法的重载(overload)和覆盖(override)
方法的重载(overload)和覆盖(override) 有的时候,类的同一种功能有多种实现方式,到底采用哪种实现方式,取决于调用者给定的参数.例如我们最常用的System.out.println() ...
- nginx 几个常用的标准模块介绍
ngx_http_ssl_module(https) 1:指明是否启用的虚拟主机的ssl功能 ssl on | off; 2:指明虚拟主机使用的证书文件 ssl_certificate /usr/lo ...
- PAT 甲级 1077 Kuchiguse
https://pintia.cn/problem-sets/994805342720868352/problems/994805390896644096 The Japanese language ...
- 解决java图形界面label中文乱码
第一:在你的具有main函数的类也即你应用运行的主类上点击右键,选择Run As中的Run Configurations,如下图:java,awt,中文方框,中文乱码第二,在Arguments标签下的 ...
- 设计模式PHP篇(二)————工厂模式
一个很简单的工厂模式.代码如下: <?php interface Person { public function sex(); } class Man implements Person { ...