在建图的时候对原图进行加边 建立一个超级源点~

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
const int maxm=;
const int inf=1e9;
struct edge {
int u;
int v;
int w;
}Edge[maxm];
int pre[maxn];
int id[maxn];
int visit[maxn];
int in[maxn];
int N,M;
int root;
int pos;
int num;
void add (int u,int v,int w) {
Edge[num].u=u;
Edge[num].v=v;
Edge[num++].w=w;
}
int solve () {
int ans=;
while () {
for (int i=;i<N;i++) in[i]=inf;
for (int i=;i<num;i++) {
int u=Edge[i].u;
int v=Edge[i].v;
if (Edge[i].w<in[v]&&u!=v) {
pre[v]=u;
in[v]=Edge[i].w;
if (u==root) pos=i;
}
}
for (int i=;i<N;i++) {
if (i!=root&&in[i]==inf) return -;
}
int tn=;
memset(id,-,sizeof(id));
memset(visit,-,sizeof(visit));
in[root]=;
for (int i=;i<N;i++) {
ans+=in[i];
int v=i;
while (visit[v]!=i&&id[v]==-&&v!=root) {
visit[v]=i;
v=pre[v];
}
if (v!=root&&id[v]==-) {
for (int u=pre[v];u!=v;u=pre[u])
id[u]=tn;
id[v]=tn++;
}
}
if (tn==) break;
for (int i=;i<N;i++) {
if (id[i]==-) id[i]=tn++;
}
for (int i=;i<num;i++) {
int v=Edge[i].v;
Edge[i].u=id[Edge[i].u];
Edge[i].v=id[Edge[i].v];
if (Edge[i].u!=Edge[i].v) Edge[i].w-=in[v];
}
N=tn;
root=id[root];
}
return ans;
}
int main () {
while (~scanf("%d%d",&N,&M)) {
int sum=;
num=;
for (int i=;i<M;i++) {
int u,v,w;
scanf ("%d %d %d",&u,&v,&w);
sum+=w;
add(u,v,w);
}
sum++;
for (int i=;i<N;i++) {
add(N,i,sum);
}
N++;
root=N-;
int ans=solve();
if (ans==-||ans>=*sum) {
printf ("impossible\n");
}
else printf ("%d %d\n",ans-sum,pos-M);
printf ("\n");
}
return ;
}

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

  2. hdu2121 Ice_cream’s world II 最小树形图(难)

    这题比HDU4009要难一些.做了4009,大概知道了最小树形图的解法.拿到这题,最直接的想法是暴力.n个点试过去,每个都拿来做一次根.最后WA了,估计是超时了.(很多题都是TLE说成WA,用了G++ ...

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

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

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

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

  5. hdu2121 Ice_cream's world II

    hdu2121 Ice_cream's world II 给一个有向图,求最小树形图,并输出根节点 \(n\leq10^3,\ m\leq10^4\) 最小树形图 对于求无根最小树形图,可以建一个虚拟 ...

  6. hdu2121 - Ice_cream’s world II(朱刘算法,不固定根)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目意思大概是要你在一些城市中选一个做首都 , 要求首都都能到其他城市 , 道路花费要最少 , ...

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

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

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

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

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

随机推荐

  1. 551-学生出勤记录 I

    551-学生出勤记录 I 给定一个字符串来代表一个学生的出勤记录,这个记录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个学 ...

  2. json 的key值不能是变量

    var _key = name; var _value = 2; var params = { _key :_ value } _key 为变量 console.log(params); { _key ...

  3. RHEL/CentOS 7中Nginx的systemd service

    源码安装的nginx ,没有systemd service 管理 nginx 下面教程,告诉你如何设置nginx 的systemd service nginx systemd的服务文件是/usr/li ...

  4. JUC-JUC强大的辅助类讲解(Semaphore、CyclicBarrier、CountDownLatch)

    一.CountDownLatch 减少计数 1.原理 * CountDownLatch主要有两个方法,当一个或多个线程调用await方法时,这些线程会阻塞. * 其它线程调用countDown方法会将 ...

  5. msfconsole启动失败并报错`not_after=': bignum too big to convert into `long'的解决方法

    1.启动msfconsole失败并报如下错误: /usr/share/metasploit-framework/lib/msf/core/payload/android.rb:86:in `not_a ...

  6. element模态框dialog中的select组件中选中无反应无显示

    https://blog.csdn.net/PGguoqi/article/details/90240650 在vue里,当你对一个不存在的属性或对象直接“.”进行赋值,或者对数组不存在的索引项直接用 ...

  7. 【Python】遍历循环

  8. EF的延迟加载LazyLoad

    延迟加载只对 关联/导航 属性(Navigation Property)有用,普通属性没有这个东西. 延迟加载是一条一条的读取属性,调用一次,读取一次. 条件: context.Configurati ...

  9. ICPC2019 亚洲区域赛 南京站

    蒟蒻终于打完了人生的第一场ICPC了. 终榜去星后rank36,AG,和AU差几十罚时了. 虽有遗憾但总体也是正常发挥了. 不愿再去对比赛做什么回顾,甚至很不愿去想.很多题已经在能力之外,即便是平常熟 ...

  10. 快速将Navicat中数据表信息导出

    1.使用navicat工具  2.新建查询  SELECT COLUMN_NAME 字段名, COLUMN_TYPE 数据类型, DATA_TYPE 字段类型, CHARACTER_MAXIMUM_L ...