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条边的有向 ...
随机推荐
- HttpWebRequest
同步请求===================================================================================== byte[] da ...
- nodejs phantom add click event
page.evaluate( function() { // find element to send click to var element = document.querySelector( ' ...
- sql之独立子查询和相关子查询总结
1.独立子查询:顾名思义:就是子查询和外层查询不存在任何联系,是独立于外层查询的: 下面就看一个例子: 有一张订单表 Sales.Order 和一张 客户表 Sales.Customer 下面的sql ...
- java和php实现RSA加密互通-b
java和PHP RSA加密实现互通 1:通过openssl 生成公钥和密钥文件(linux) (1) 生产私钥文件命令 openssl genrsa -out rsa_private_key.pe ...
- vs2008+cmake2.8+OpenCV2.8.4配置过程中OpenCV.sln重编译部分工程失败
解决方法来自此链接 http://www.tuicool.com/articles/qiQBb2N vs2008+cmake2.8+OpenCV2.8.4配置过程 1.解压opencv2.4.8 2. ...
- centos7安装mplayer以及出现的各种问题
首先,centos7默认的视频播放器基本不能用,这里我们选择mplayer作为视频播放器. 安装的过程,痛并快乐着....... 首先我们去mplayer的官网下载需要的文件,http://www.m ...
- shuffle过程中的信息传递
依据Spark1.4版 Spark中的shuffle大概是这么个过程:map端把map输出写成本地文件,reduce端去读取这些文件,然后执行reduce操作. 那么,问题来了: reducer是怎么 ...
- Notifications Nagios
Introduction I've had a lot of questions as to exactly how notifications work. This will attempt to ...
- UVA548——Tree(中后序建树+DFS)
Tree You are to determine the value of the leaf node in a given binary tree that is the terminal nod ...
- WCF的通信
[ServiceContract] public interface IContractDemoOne { [OperationContract(IsOneWay=true) ...