HDU 2121 Ice_cream’s world II 最小树形图
这个题就是需要求整个有向带权图的最小树形图,没有指定根,那就需要加一个虚根
这个虚根到每个点的权值是总权值+1,然后就可以求了,如果求出来的权值大于等于二倍的总权值,就无解
有解的情况,还需要输出最根,多解的情况,肯定是某个环上所有的点为根都可以(比如所有的点构成一个环),
这样加边的时候虚边的时候按照点的标号从小到大编,这样第一个以虚根为前驱的点也是最小的点就可以标记(标记一下)
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
typedef long long LL;
const int N=2e3+;
const LL INF=0x7fffffff;
struct Edge{
int u,v;
LL w;
}edge[N*N];
LL in[N];
int id[N],vis[N],pre[N],n,m,pos;
LL zhuliu(int rt,int n,int m){
LL ret=;
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].w<in[edge[i].v]){
if(edge[i].u==rt)pos=i;
pre[edge[i].v]=edge[i].u;
in[edge[i].v]=edge[i].w;
}
}
for(int i=;i<=n;++i)
if(i!=rt&&in[i]==INF)return -;
int cnt=-;
memset(id,-,sizeof(id));
memset(vis,-,sizeof(vis));
in[rt]=;
for(int i=;i<=n;++i){
ret+=in[i];
int v=i;
while(vis[v]!=i&&id[v]==-&&v!=rt){
vis[v]=i;
v=pre[v];
}
if(v!=rt&&id[v]==-){
++cnt;
for(int u=pre[v];u!=v;u=pre[u])
id[u]=cnt;
id[v]=cnt;
}
}
if(cnt==-)break;
for(int i=;i<=n;++i)
if(id[i]==-)id[i]=++cnt;
for(int i=;i<=m;++i){
int u=edge[i].u,v=edge[i].v;
edge[i].u=id[u];
edge[i].v=id[v];
if(id[u]!=id[v])edge[i].w-=in[v];
}
n=cnt;
rt=id[rt];
}
return ret;
}
int main()
{
while(~scanf("%d%d",&n,&m)){
LL sum=;
for(int i=;i<=m;++i){
scanf("%d%d%I64d",&edge[i].u,&edge[i].v,&edge[i].w);
++edge[i].u,++edge[i].v;
sum+=edge[i].w;
}
++sum;
for(int i=m+;i<=m+n;++i){
edge[i].u=,edge[i].v=i-m;
edge[i].w=sum;
}
LL ans=zhuliu(,n,n+m);
if(ans-sum>=sum)printf("impossible\n\n");
else printf("%I64d %d\n\n",ans-sum,pos-m-);
}
return ;
}
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 ...
- 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 ...
- 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 (无定根最小树形图)
题目链接: 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
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 最小树形图(难)
这题比HDU4009要难一些.做了4009,大概知道了最小树形图的解法.拿到这题,最直接的想法是暴力.n个点试过去,每个都拿来做一次根.最后WA了,估计是超时了.(很多题都是TLE说成WA,用了G++ ...
- hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】
称号:pid=2121" target="_blank">hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向 ...
随机推荐
- 使用Yeoman搭建 AngularJS 应用 (7) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/preview-inbrowser.html 开启你的服务 运行Grunt任务,通过输入下面的命令来创建一个本地Node的http服务,地址 ...
- 读书笔记 (一) ———Fundamentals of Multiagent Systems with NetLogo Examples by Prof. Jose M Vidal
在网上发现Prof. Jose M Vidal用NetLogo仿真Multi-agent system的视频,随后下载他的著作Fundamentals of Multiagent Systems wi ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- jQuery的优点——(一)
稍微学点js然后就用起jQuery,用久了,就好像自己都成高手了,想得少了,都觉得自己就是js高手了.哗啦啦的惭愧啊=.= 面试GY的时候,因为在简历上写着大大的会用jQuery快速开发原型页面,然后 ...
- [转载]介绍一个JSONP 跨域访问代理API-yahooapis
你是否遇到了想利用AJAX访问一些公网API,但是你又不想建立自己的代理服务,因为有时我根本就没打算涉及服务端任何代码,但是讨厌的浏览器的同源策略,阻止了我们的ajax调用. 比如我想访问一个天气的r ...
- The 11th Zhejiang Provincial Collegiate Programming Contest->Problem A:A - Pokemon Master
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3776 题意:比较两组数据的总和大小. #include <iostr ...
- Unity3d + NGUI 的多分辨率适配(黑边)
原地址:http://www.2cto.com/kf/201310/250921.html 一.当下移动设备的主流分辨率(数据来自“腾讯分析移动设备屏幕分辨率分析报告”) 1.1 iOS设备的分辨率主 ...
- python top project of 2013
Hi Pythonistas! 测试和调试 Testing & Debugging 框架及Web Frameworks & Web 并发 Concurrency 任务调度 Job Sc ...
- BZOJ 4311 向量
shallot+向量集 混合版? 首先我们考虑每个向量的存在时间为[L,R] 那么我们知道任意一个区间在线段树上最多被分解成logn个区间 那么我们可以像shallot一样进行区间覆盖 注意到本题的查 ...
- [译]C++书籍终极推荐
转载声明: 翻译仅以技术学习和交流为目的,如需转载请务必标明原帖链接. 来源:http://stackoverflow.com/questions/388242/the-definitive-c-bo ...