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

#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. Wannafly Camp 2020 Day 6K 最大权值排列

    按照样例那样排列即可 #include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; if( ...

  2. eclipse unable to start within 45 seconds

    在eclipse4.8.2中运行tomcat8.5项目时,提示出错: Server Tomcat v8.0 Server at localhost was unable to start within ...

  3. execute、executeUpdate、executeQuery的区别

    链接:https://blog.csdn.net/u012501054/article/details/80323176 链接:https://blog.csdn.net/CNAHYZ/article ...

  4. nginx配置之后接口状态200,但是无返回数据问题小记

    nginx配置可能有问题.导致nginx不能解析PHP文件,检测nginx里对于php的配置信息. location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; f ...

  5. 利用Master库spt_values表 连续数字

    SELECT CONVERT ( ), dateadd(d, number, GETDATE()), ) AS every_time FROM master..spt_values n WHERE n ...

  6. 2018护网杯easy_tornado(SSTI tornado render模板注入)

    考点:SSTI注入 原理: tornado render是python中的一个渲染函数,也就是一种模板,通过调用的参数不同,生成不同的网页,如果用户对render内容可控,不仅可以注入XSS代码,而且 ...

  7. VS+QT创建一个OpenCV应用

    1.选择:文件->新建->项目->搜索“QT”->Qt GUI Application,输入工程名,下一步…进行新建. 选择依赖的模块:最基础的QtCore.QtGui.QtW ...

  8. springboot静态资源

    https://blog.csdn.net/yali_aini/article/details/83213695 https://blog.csdn.net/sihai12345/article/de ...

  9. 1052 Linked List Sorting (25分)

    题目 1. 思路 使用map存放所有的地址对 使用起始地址遍历map,结果存放在vector中 排序vector 输出vector 2. 注意点 开始的时候起始地址为-1 可能有些节点没有用到,注意排 ...

  10. .htaccess详解

    http://www.cnblogs.com/adforce/archive/2012/11/23/2784664.html .htaccess是什么 .htaccess文件(或者"分布式配 ...