POJ 3164 Command Network 最小树形图模板
最小树形图求的是有向图的最小生成树,跟无向图求最小生成树有很大的区别。
步骤大致如下:
1.求除了根节点以外每个节点的最小入边,记录前驱
2.判断除了根节点,是否每个节点都有入边,如果存在没有入边的点,说明树形图不存在,退出。
3.沿着节点的前驱找,如果发现环,把环缩点。
4.如果不存在环,结束。否则,跳到1.
其实我是来贴模板的。。。
比较好的讲解:最小树型图的求解与实现
代码讲解比较好的:hdu4009 Transfer water ( 最小树形图的模板 )
用IO优化时死活TLE,改了scanf就过了,后来用!=EOF发现数据不全,怪不得TLE了,囧
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ char IN;
bool NEG;
inline void Int(int &x){
NEG = 0;
while(!isdigit(IN=getchar()))
if(IN=='-')NEG = 1;
x = IN-'0';
while(isdigit(IN=getchar()))
x = x*10+IN-'0';
if(NEG)x = -x;
}
inline void LL(ll &x){
NEG = 0;
while(!isdigit(IN=getchar()))
if(IN=='-')NEG = 1;
x = IN-'0';
while(isdigit(IN=getchar()))
x = x*10+IN-'0';
if(NEG)x = -x;
} /******** program ********************/ const int MAXN = 105;
const double INF = 1e50; int pre[MAXN],id[MAXN],use[MAXN];
int px[MAXN],py[MAXN];
double in[MAXN]; struct Edge{
int x,y;
double cost;
Edge(){}
Edge(int _x,int _y,double _cost):x(_x),y(_y),cost(_cost){}
}g[MAXN*MAXN];
double treeDiagrm(int root,int n,int m){
double ans = 0;
while(true){
rep1(i,n)
in[i] = INF; rep1(i,m){
int x = g[i].x , y = g[i].y;
double cost = g[i].cost;
if(cost<in[y]&&y!=x)
pre[y] = x , in[y] = cost;
}
in[root] = 0 , pre[root] = root;
rep1(x,n){
if(in[x]>1e20)return -1;
ans += in[x];
}
Clear(use);
Clear(id);
int tot = 0;
rep1(i,n){
if(use[i])continue;
int x = i;
while(!use[x])use[x] = i , x = pre[x];
if(use[x]!=i||x==root)continue;
id[x] = ++ tot;
for(int t=pre[x];t!=x;t=pre[t])
id[t] = tot;
}
if(!tot)break;
rep1(i,n)if(!id[i])id[i] = ++ tot; rep1(i,m){
g[i].cost -= in[g[i].y];
g[i].x = id[g[i].x];
g[i].y = id[g[i].y];
}
n = tot;
root = id[root];
}
return ans;
} double cal(int x,int y){
return sqrt(0.0+x*x+y*y);
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif int x,y,n,m;
while(~RD2(n,m)){
rep1(i,n)
RD2(px[i],py[i]);
rep1(i,m){
RD2(x,y);
g[i] = Edge(x,y,cal(px[x]-px[y],py[x]-py[y]));
}
double ans = treeDiagrm(1,n,m);
if(ans>0)
printf("%.2lf\n",ans);
else puts("poor snoopy");
} return 0;
}
POJ 3164 Command Network 最小树形图模板的更多相关文章
- POJ 3164 Command Network 最小树形图
题目链接: 题目 Command Network Time Limit: 1000MS Memory Limit: 131072K 问题描述 After a long lasting war on w ...
- POJ 3164 Command Network 最小树形图 朱刘算法
=============== 分割线之下摘自Sasuke_SCUT的blog============= 最 小树形图,就是给有向带权图中指定一个特殊的点root,求一棵以root为根的有向生成树T, ...
- POJ3436 Command Network [最小树形图]
POJ3436 Command Network 最小树形图裸题 傻逼poj回我青春 wa wa wa 的原因竟然是需要%.2f而不是.2lf 我还有英语作业音乐作业写不完了啊啊啊啊啊啊啊啊啊 #inc ...
- poj 3164 Command Network
http://poj.org/problem?id=3164 第一次做最小树形图,看着别人的博客写,还没弄懂具体的什么意思. #include <cstdio> #include < ...
- POJ 3164 Command Network(最小树形图模板题+详解)
http://poj.org/problem?id=3164 题意: 求最小树形图. 思路: 套模板. 引用一下来自大神博客的讲解:http://www.cnblogs.com/acjiumeng/p ...
- poj 3164 Command Network(最小树形图模板)
Command Network http://poj.org/problem?id=3164 Time Limit: 1000MS Memory Limit: 131072K Total Subm ...
- POJ 3164 Command Network (最小树形图)
[题目链接]http://poj.org/problem?id=3164 [解题思路]百度百科:最小树形图 ]里面有详细的解释,而Notonlysucess有精简的模板,下文有对其模板的一点解释,前提 ...
- POJ 3164——Command Network——————【最小树形图、固定根】
Command Network Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 15080 Accepted: 4331 ...
- POJ 3164 Command Network ( 最小树形图 朱刘算法)
题目链接 Description After a long lasting war on words, a war on arms finally breaks out between littlek ...
随机推荐
- 【转】android onNewIntent()触发机制及注意事项
一.onNewIntent() 在IntentActivity中重写下列方法:onCreate onStart onRestart onResume onPause onStop onDestro ...
- Java内存区域分析
程序计数器 指令运行的指示器. 每一个线程都有独立的程序计数器,互无影响,我们称这类区域为线程私有的内存. 运行Java方法,计数器记录的是正在运行的虚拟机字节码指令地址;假设运行的是native方法 ...
- 四月二十五日,bugzilla for CentOS 安装
Bugzilla for CentOS 5.4 制作人,陈浩 时间:2014.4.25 原创 文件夹 Bugzilla for CentOS 5.4 一. 装系统 1) 新建虚拟机 15G硬盘,51 ...
- 转换到 COFF 期间失败: 文件无效或损坏 解决方法
转自csdn 终极解决方案:VS2010在经历一些更新后,建立Win32 Console Project时会出“error LNK1123” 错误,解决方案为将 项目|项目属性|配置属性|清单工具|输 ...
- winForm 程序开发界面参数传递
1. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; u ...
- [置顶] Android开发之MediaPlayerService服务详解(一)
前面一节我们分析了Binder通信相关的两个重要类:ProcessState 和 IPCThreadState.ProcessState负责打开Binder 驱动,每个进程只有一个.而 IPCThre ...
- linux C 9*9
#include<stdio.h> #include<conio.h> #include <windows.h> void Gotoxy(int x, int y) ...
- 深刻理解Java中final的作用(一):从final的作用剖析String被设计成不可变类的深层原因
声明:本博客为原创博客,未经同意,不得转载!小伙伴们假设是在别的地方看到的话,建议还是来csdn上看吧(原文链接为http://blog.csdn.net/bettarwang/article/det ...
- Redis_字典
阅读本文之前要了解的两件事情,第一,Redis是一种Key-Value数据库,第二,字典是一种保存键值对的抽象数据结构.所以不难猜出字典在Redis中应用一定很广泛,实际上,Redis数据库的底层实现 ...
- 一行命令搞定VS2012无法安装cocos2d-x-2.1.4及创建跨平台项目(二)
转自:http://blog.csdn.net/yangjingui/article/details/9418843 由于上次发了一个比较二的方法来解决VS2012无法安装cocos2d-x-2.1. ...