HDU - 2121 :http://acm.hdu.edu.cn/showproblem.php?pid=2121

比较好的朱刘算法blog:https://blog.csdn.net/txl199106/article/details/62045479

题意:

  在一个有向图中,找一个点,使得这个点到其他点的距离和最小,输出距离和,和这个点的坐标。

思路:

  无根最小树形图,设所有的有向图的距离和为sum。自己建立一个虚拟的原点(n+1),向每一个节点连一条距离为sum+1的边。以n+1为根结点跑一遍最小树形图(复杂度O(VE)),如果求出的ans == -1 或者 ans >=2*(sum + 1),无解,因为这么大的ans,只可能用了两条我们自己建立的边。由于每条边的端点在跑最小树形图的时候会改变,所以记录这是第几条边rtt,结果就是rtt - m,代码中由减了1是因为原图是Base0的。

/*
* @Author: chenkexing
* @Date: 2018-09-05 11:05:14
* @Last Modified by: chenkexing
* @Last Modified time: 2018-09-10 20:21:22
*/
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxm = ;
const int maxn = ;
struct Edge
{
int from,to,c;
}e[maxm];
int in[maxn],vis[maxn],pre[maxn],id[maxn];
int rtt;
int zhuliu(int root,int n,int m){
int res = ;
while(true){
memset(in, inf, sizeof(in));
for(int i=; i<=m; i++){
if(e[i].from != e[i].to && e[i].c < in[e[i].to]){
pre[e[i].to] = e[i].from;
in[e[i].to] = e[i].c;
if(e[i].from == root)rtt = i;
}
}
for(int i=; i<=n; i++){
if(i!=root&&in[i] == inf)
return -;
}
int tn = ,v;
memset(id,-,sizeof(id));
memset(vis,-,sizeof(vis)); in[root] = ;
for(int i=; i<=n; i++){
res += in[i];
v = i;
while(v !=root && id[v] == - && vis[v] != i){
vis[v] = i;
v = pre[v];
}
if(v!=root && id[v] == -){
id[v] = ++tn;
for(int u = pre[v]; u!=v; u = pre[u]){
id[u] = tn;
}
}
}
if(tn == )break;
for(int i=; i<=n; i++){
if(id[i] == -)id[i] = ++tn;
} for(int i=; i<=m; i++){
int v = e[i].to;
e[i].to = id[e[i].to];
e[i].from = id[e[i].from];
if(e[i].to != e[i].from){
e[i].c -= in[v];
}
} n = tn;root = id[root];
}
return res; }
int main(){
int n,m,r;
while(~scanf("%d%d", &n, &m))
{
int sum = ;
for(int i=; i<=m; i++){
int u,v,c;
scanf("%d%d%d", &u, &v, &c);
u++,v++;
e[i].from = u;e[i].to = v;
e[i].c = c;
sum += c;
}
sum++;
for(int i=m+; i<=n+m; i++){
e[i].from = n+;
e[i].to = i-m;
e[i].c = sum;
} int ans = zhuliu(n+,n+,n+m);
// debug(ans);
if(ans == - || ans - sum >= sum){
puts("impossible");
}
else {
printf("%d %d\n", ans - sum, rtt - m - );
}
printf("\n");
} return ;
}

HDU - 2121

HDU - 2121 Ice_cream’s world II 无根最小树形图的更多相关文章

  1. hdu 2121 Ice_cream’s world II (无定根最小树形图)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目大意: 有n个点,有m条单向路,问这n个点组成最小树形图的最小花费. 解题思路: 1:构造 ...

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

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

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

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

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

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

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

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

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

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

  8. hdu 2121无根最小树形图要建一个虚拟节点

    #include<stdio.h> #include<string.h> #define inf 999999999 #define N 1100 struct node { ...

  9. hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】

    称号:pid=2121" target="_blank">hdoj 2121 Ice_cream's world II 题意:题目是一道躶题,给n个点,m条边的有向 ...

随机推荐

  1. c&c服务器(command and control server)

    远程命令和控制服务器,目标机器可以接收来自服务器的命令,从而达到服务器控制目标机器的目的.该方法常用于病毒木马控制被感染的机器.

  2. JDBC秒变C3P0连接池——再加连接解耦

    从JDBC连接到C3P0数据库连接池 在Java开发中,使用JDBC操作数据库的四个步骤如下:   ①加载数据库驱动程序(Class.forName("数据库驱动类");)   ② ...

  3. 【Android】Android sdk content loader 0%

    前天用 Eclipse 突然遇到了这个问题...重启了好几次都不行,百度了一下,原来之前有不少人遇到过.后来找到了一篇文章,其中的方法二(如下): 方法二(关闭后,拔网线再重启): 如果用最省事的方法 ...

  4. Scala的常用小技巧

    1."RichString.java".stripSuffix(".java") == "RichString" "http:// ...

  5. Downgrade extraction on phones running Android 7/8/9

    Now it's more and more difficult for forensic tools to extract evidence from smartphone running Andr ...

  6. SQLServer2000同步复制技术实现步骤

    SQLServer2000同步复制技术实现步骤 一. 预备工作 1.发布服务器,订阅服务器都创建一个同名的windows用户,并设置相同的密码,做为发布快照文件夹的有效访问用户 --管理工具 --计算 ...

  7. 在 dotnet core (C#)下的颜色渐变

    直接使用等比例抽样算法,连同透明度一起计算. public IList<Color> ShadeColors(Color c1, Color c2, int resultCount) { ...

  8. 【一些小常识】Linux文件目录的通配符用法/*

    在使用linux命令的时候,一时有点搞不清*的用法,于是整理记录下,在做jenkins 持续集成时还是很有用的 “*”在通配符中是最常用的一种,主要整理下在使用Linux命令时,文件夹目录的用法. 1 ...

  9. luogu1220_关路灯 区间dp

    传送门 区间dp f[i][j][state] : [i, j]区间 state=0 当前选i state = 1 当前选j 注意枚举的顺序 转移的设计时 在同时刻不在[i,j]区间里的数也要考虑 不 ...

  10. ZDog:简单便捷好玩的的3D设计和动画制作库

    各位老铁,我灰太狼又又又回来了,嘿嘿!!!!最近在忙所以有日子没写博客了,今天带大家看个好玩的东西 这个东西是今天偶尔看到的,是啥呢,难道是漂亮的小姐姐吗?当然是......不可能的了,这个东西其实就 ...