HDU2121 Ice_cream’s world II (最小树形图)
在建图的时候对原图进行加边 建立一个超级源点~
#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 (最小树形图)的更多相关文章
- 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 ...
- hdu2121 Ice_cream’s world II 最小树形图(难)
这题比HDU4009要难一些.做了4009,大概知道了最小树形图的解法.拿到这题,最直接的想法是暴力.n个点试过去,每个都拿来做一次根.最后WA了,估计是超时了.(很多题都是TLE说成WA,用了G++ ...
- 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
hdu2121 Ice_cream's world II 给一个有向图,求最小树形图,并输出根节点 \(n\leq10^3,\ m\leq10^4\) 最小树形图 对于求无根最小树形图,可以建一个虚拟 ...
- hdu2121 - Ice_cream’s world II(朱刘算法,不固定根)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目意思大概是要你在一些城市中选一个做首都 , 要求首都都能到其他城市 , 道路花费要最少 , ...
- 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——————【最小树形图、不定根】
Ice_cream’s world II Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- HDU ACM 2121 Ice_cream’s world II (无根最小树形图)
[解题思路]这题先看了NotOnlySuccess的解题思路,即设置虚根再处理的做法:弄了一个上午,再次有种赶脚的感觉~~如果需要找出为什么需要去比所有权值之和更大的数为新增的虚边的话,一开始我理解仅 ...
随机推荐
- Prettier - Code formatter使用
更多VSCode插件使用请访问:VSCode常用插件汇总 Prettier - Code formatter这是一款 格式化js.css代码插件,暂不解释. Prettier是什么? Prettier ...
- 倍加福r2000激光驱动安装及使用
第一次完成激光的驱动安装和激光数据的采集,遇到很多问题,最后一一解决,现在记录下来,方便后面給机器人安装激光使用 激光的了解 (1)激光型号:OMD30M-R2000(30M表示30m) (2)参考手 ...
- linq 查询-“必须是可缩小的节点”
出现此错误的情况 查询中先使用分组方法GroupBy()=>Select()=>OrderBy()=>ToList()(实现分组后排序) 解决方法: 排序OrderBy()应该放在S ...
- [转]shallow heap & retained heap
所有包含Heap Profling功能的工具(MAT, Yourkit, JProfiler, TPTP等)都会使用到两个名词,一个是Shallow Size,另一个是 Retained Size. ...
- adb server version (xx) doesn't match this client (xx); killing...
问题 查看AndroidSDK的adb版本 查看模拟器adb的版本号 安装路径/bin目录下的 nox_adb.exe 将AndroidSDK的adb复制出来,重命名为nox_adb.exe,覆盖模拟 ...
- selenium爬去数据+存储
1 爬去数据代码 #coding=utf-8 from selenium import webdriver from selenium.webdriver.common.by import By fr ...
- date-fns时间库的基本使用
在react中使用date-fns: import sub_days from 'date-fns/sub_days'; import start_of_week from 'date-fns/sta ...
- python 多版本环境
参考 https://www.cnblogs.com/---JoyceLiuHome/articles/7852871.html 安装 Anaconda集成化环境 https://www.anacon ...
- Python连接操作数据库
步骤: 1.创建与数据库的连接对象: 2.创建游标: 3.通过游标执行语句 4.增删改需要提交(commit)数据 5.关闭连接 如: import MySQLdb # Python通过MySQL ...
- 后端——框架——缓存框架——memcached——《Memcached教程》阅读笔记
Memcached的知识点大致可以分为三个部分. 服务器部分:环境搭建. 概念:存储的数据类型,指令,内存的替换策略. 集成:与Java语言的集成. 1.搭建环境 1.1 Linux环境 在Linux ...