HDU 2121——Ice_cream’s world II——————【最小树形图、不定根】
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
Sample Output
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef long long INT;
const int maxn = 1100;
const int INF = 0x3f3f3f3f;
struct Edge{
int from,to;
int dist;
}edges[maxn*maxn];
int pre[maxn],vis[maxn],ID[maxn];
int In[maxn];
int ansidx ;
INT Zhuliu(int root,int n,int m){
INT ret = 0;
int u,v;
while(true){
for(int i = 0; i < n; i++){
In[i] = INF;
}
for(int i = 0; i < m; i++){
Edge &e = edges[i];
u = e.from; v = e.to;
if(In[v] > e.dist && u != v){
pre[v] = u;
if(u == root){ //记录边的编号,这个编号-m就是点编号,因为我们加边是从顶点从小到大
ansidx = i;
}
In[v] = e.dist;
}
}
for(int i = 0; i < n; i++){
if(i == root) continue;
if(In[i] == INF)
return -1;
}
In[root] = 0;
int cntcir = 0;
memset(vis,-1,sizeof(vis));
memset(ID,-1,sizeof(ID));
for(int i = 0; i < n; i++){
ret += In[i];
v = i;
while(vis[v]!= i && ID[v] ==-1 &&v != root){
vis[v] = i;
v = pre[v];
}
if(v != root && ID[v] == -1){
for(u = pre[v]; u != v; u = pre[u]){
ID[u] = cntcir;
}
ID[v] = cntcir++;
}
}
if(cntcir == 0){
break;
}
for(int i = 0; i < n; i++){
if(ID[i]==-1){
ID[i] = cntcir++;
}
}
for(int i = 0; i < m; i++){
v = edges[i].to;
Edge & e = edges[i];
e.from = ID[e.from];
e.to = ID[e.to];
if(e.from != e.to){
e.dist -= In[v];
}
}
n = cntcir;
root = ID[root];
}
return ret;
}
int main(){
int n,m, T, cas = 0;
while(scanf("%d%d",&n,&m)!=EOF){
int a,b,c;
INT sumd = 0;
for(int i = 0; i < m; i++){
scanf("%d%d%d",&a,&b,&c);
a++,b++;
edges[i].from = a;
edges[i].to = b;
if(a == b){
edges[i].dist = INF;
continue;
}
edges[i].dist = c;
sumd += c;
}
for(int i = 0; i < n;i++){
edges[m+i].from = 0;
edges[m+i].to = i + 1;
edges[m+i].dist = sumd + 1;
}
INT res = Zhuliu(0,n+1,m+n);
if(res == -1||res > 2*sumd+1){
puts("impossible");
}else{
printf("%lld %d\n",res - sumd -1,ansidx - m);
}puts("");
}
return 0;
} /*
3 2
0 1 2
1 2 3 3 0 */
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 最小树形图
这个题就是需要求整个有向带权图的最小树形图,没有指定根,那就需要加一个虚根 这个虚根到每个点的权值是总权值+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 ...
- 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条边的有向 ...
随机推荐
- Django会话,用户和注册之session
鉴于cookie的不安全,django自带的session框架会帮我们搞定这些问题 你可以用session 框架来存取每个访问者任意数据, 这些数据在服务器端存储,并对cookie的收发进行了抽象. ...
- [内容分享]粗略判断Shader每条代码的成本
https://mp.weixin.qq.com/s/Vyn1bKaBMHommxbnFPPQeg Unity对Shader文件进行编译的时候,DX9和DX11的版本会直接生成汇编码. ? len ...
- asp如何让panel居中
把panel放在一个table的td中,并把position设为relative,就可以在设计中直接调整它的位置了
- 【转】windows server 2012 R2搭建IIS服务器
源地址:http://blog.csdn.net/microsoft_wu/article/details/46521017
- NOIP2018初赛总结(提高组)(试题+答案+简要解析)
NOIP2018初赛总结(提高组) 更新完毕(纯手敲),如果有错误请在下面留言 单选题 T1.下列四个不同进制的数中,与其它三项数值上不相等的是 A.\((269)_{16}\) B.\((617)_ ...
- PHP删除目录下的空目录
function rm_empty_dir($path){ if(is_dir($path) && ($handle = opendir($path))!==false){ ...
- java.sql.Date/ java.util.Date/ java.util.Calendar 用法与区别
在 JDK API 这样解释," 在 JDK 1.1 之前,类 Date 有两个其他的函数.它允许把日期解释为年.月.日.小时.分钟和秒值.它也允许格式化和解析日期字符串.不过,这些函数的 ...
- Android模拟器手动设置经纬度坐标
第一种方式可以在eclipse的DDMS中的Emulator control中设置,如下图 另一种是在cmd中输入telnet localhost 5554(注:5554是模拟器在本机的端口,有可能不 ...
- Mybatis学习笔记(五) —— Mapper.xml(输入映射和输出映射)
一.parameterType(输入类型) 1.1 传递简单类型 <!-- 根据用户id查询用户 --> <select id="queryUserById" p ...
- eclipse gradle 找不到依赖解决办法
右击工程,选择gradle 在点击Refresh Gradle Project 即可,..不得不说,gradle 在eclipse 下真没maven 好用.....