称号:

pid=2121" target="_blank">hdoj 2121 Ice_cream’s world II

题意:题目是一道躶题,给n个点,m条边的有向图。然后找一个点。到全部点的距离和最小。找出这个点并输入距离。

分析:非常明显是求一个最小树形图,可是没有说根节点。要找跟节点,我们能够虚拟一个节 点 x 。x 到全部节点连边距离为前面全部距离和+1为 dis 。

然后从x 节点求一次最小树形图为ans,则ans - dis 就是最小树形图的距离。

假设图不连通,或者ans》=2*dis 说明不存在,都则与 x 点出发的边就是结果点

AC代码:

#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <vector>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 1100;
const int inf = 0x3f3f3f3f;
int n,m;
struct Node
{
int from,to,dis;
};
vector<Node> e;
int ha[N],vis[N],father[N],in[N];
int Minroot;
int zhuliu(int root)
{
int ans = 0;
while(true)
{
for(int i=0;i<n;i++)
in[i] = inf;
memset(father,-1,sizeof(father));
for(int i=0; i<e.size(); i++) //找最小入边
{
int to = e[i].to;
if(e[i].dis<in[to] && e[i].from!=e[i].to)
{
in[to] = e[i].dis;
father[to] = e[i].from;
if(e[i].from == root) //找最小
Minroot = i;
}
}
for(int i=0;i<n;i++)
{//printf("%d ",in[i]);
if(i!=root && in[i]==inf)
return -1;
}
int cnt = 0;
in[root] = 0;
memset(ha,-1,sizeof(ha));
memset(vis,-1,sizeof(vis));
for(int i=0;i<n;i++) //找自环
{ ans += in[i];
int v = i;
while(v!=root && ha[v]==-1 && vis[v]!=i)
{
vis[v] = i;
v = father[v];
}
if(v!=root && ha[v]==-1)
{
for(int j = father[v];j != v;j=father[j])
{
ha[j] = cnt;
}
ha[v] = cnt++;
}
}
if(cnt == 0) //跳出条件
break;
for(int i=0;i<n;i++)
if(ha[i]==-1)
ha[i]=cnt++;
for(int i = 0; i< e.size();i++)
{
int tmp = e[i].to;
e[i].from = ha[e[i].from];
e[i].to = ha[e[i].to];
if(e[i].from != e[i].to)
e[i].dis -= in[tmp];
}
n = cnt;
root = ha[root];
}
return ans;
}
int main()
{
//freopen("Input.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
int num = 0;
for(int i=0;i<m;i++)
{
int x,y,s;
scanf("%d%d%d",&x,&y,&s);
e.push_back((Node){x,y,s});
num = num + s;
}
num++;
for(int i=0;i<n;i++)
e.push_back((Node){n,i,num});
n++;
int ans = zhuliu(n-1);
//printf("%d %d\n",ans,num);
if(ans==-1||ans>=2*num)
puts("impossible");
else
printf("%d %d\n",ans-num,Minroot-m);
puts("");
e.clear();
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】的更多相关文章

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

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

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

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

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

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

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

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

  6. HDU - 2121 Ice_cream’s world II 无根最小树形图

    HDU - 2121 :http://acm.hdu.edu.cn/showproblem.php?pid=2121 比较好的朱刘算法blog:https://blog.csdn.net/txl199 ...

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

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

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

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

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

随机推荐

  1. c++ 对象指针参数和对象引用参数02

    对象指针作为函数参数和对象引用作为函数参数都比对象作为函数参数要用的更为普遍 传对象指针和传对象引用作为实参,那么实参在函数里发生了变话,那么相应的对象本身也会发生变化,二传递对象本身作为实参的话,实 ...

  2. 使用SharePoint管理中心管理服务

    使用SharePoint管理中心管理服务 为了管理服务应用程序.场管理员要么使用管理中心,要么使用PowerShell. 管理服务应用程序页面列出了场上执行的服务.你能够管理他们. 很多服务都有自己的 ...

  3. EJB通过ANT提高EJB应用程序的开发效率、无状态发展本地接口bean、开发状态bean

    该jboss集成到eclipse 关掉Jboss控制台新闻Ctrl+c,在MyEclipse→Servers→Jboss可配置JBoss. 通过ANT提高EJB应用的开发效率 在HelloWorld ...

  4. Red Gate系列之六 SQL Test 1.0.12.3 Edition SQL测试工具 完全破解+使用教程

    原文:Red Gate系列之六 SQL Test 1.0.12.3 Edition SQL测试工具 完全破解+使用教程 Red Gate系列之六 SQL Test 1.0.12.3 Edition S ...

  5. Oracle SQL Lesson (9) - 操作数据(增删改)

    使用INSERT语句INSERT INTO table [(column [, column...])]VALUES (value [, value...]); INSERT INTO departm ...

  6. Unity入门

    Unity入门 用unity做一个最简单的交互.(相当于Hello World)仅仅要最后能执行就算入门了. 第一步,要先用三维制作软件制作出我们须要的场景. 这儿使用的是Max2012(软件大小3. ...

  7. virtio-blk分析

    和virtio-network相同,virtio-blk驱动程序使用Virtio机制Guest它提供了一个高性能的设备I/O方法.我们期待在这里virtio-blk实现. [点击查看全文] http: ...

  8. Could not load file or assembly&#39;System.Data.SQLite.dll&#39; or one of its depedencies

    [问题]  在我本机的开发环境c#连接sqlite3没有问题,但是release版本号移植到其它的机器就提示Could not load file or assembly'System.Data. ...

  9. delphi webbrowser 经常使用的演示样本

    var Form : IHTMLFormElement ; D:IHTMLDocument2 ; begin with WebBrowser1 do begin D := Document as IH ...

  10. git 仓库

    从 Git 删除文件 rm test.txt git rm test.txt 加入远程仓库 $ git remote origin $ git remote add pb git://github.c ...