poj 3164 Command Network
http://poj.org/problem?id=3164
第一次做最小树形图,看着别人的博客写,还没弄懂具体的什么意思。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 1000
using namespace std;
const int inf=<<; int n,m;
double g[maxn][maxn];
bool vis[maxn];
int cicl[maxn];
int pre[maxn];
struct node
{
double x,y;
}p[maxn]; double sqr(int x)
{
return x*x;
} double dis(int a,int b)
{
return sqrt(sqr(p[a].x-p[b].x)+sqr(p[a].y-p[b].y));
} void dfs(int s)
{
if(vis[s]) return ;
vis[s]=true;
for(int i=; i<=n; i++)
{
if(g[s][i]<inf)
{
dfs(i);
}
}
} bool deal()
{
dfs();
for(int i=; i<=n; i++)
{
if(!vis[i]) return false;
}
return true;
} double solve()
{
double ans=;
int i;
memset(cicl,,sizeof(cicl));
while()
{
for(i=; i<=n; i++)
{
if(cicl[i]) continue;
g[i][i]=inf;
pre[i]=i;
for(int j=; j<=n; j++)
{
if(cicl[j]) continue;
if(g[j][i]<g[pre[i]][i])
{
pre[i]=j;
}
}
}
int j;
for(i=; i<=n; i++)
{
if(cicl[i]) continue;
j=i;
memset(vis,false,sizeof(vis));
while(!vis[j]&&j!=)
{
vis[j]=true;
j=pre[j];
}
if(j==) continue;
i=j;
ans+=g[pre[i]][i];
for(j=pre[i]; j!=i; j=pre[j])
{
ans+=g[pre[j]][j];
cicl[j]=;
}
for(j=; j<=n; j++)
{
if(cicl[j]) continue;
if(g[j][i]<inf)
{
g[j][i]-=g[pre[i]][i];
}
}
for(j=pre[i]; j!=i; j=pre[j])
{
for(int k=; k<=n; k++)
{
if(cicl[k]) continue;
if(g[j][k]<inf)
{
g[i][k]=min(g[i][k],g[j][k]);
}
if(g[k][j]<inf)
{
g[k][i]=min(g[k][i],g[k][j]-g[pre[j]][j]);
}
}
}
break;
}
if(i>n)
{
for(j=; j<=n; j++)
{
if(cicl[j]) continue;
ans+=g[pre[j]][j];
}
break;
}
}
return ans;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
g[i][j]=inf;
}
}
for(int i=; i<=n; i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
for(int i=; i<m; i++)
{
int s,t;
scanf("%d%d",&s,&t);
g[s][t]=dis(s,t);
}
memset(vis,false,sizeof(vis));
if(!deal())
{
printf("poor snoopy\n");
}
else printf("%.2lf\n",solve());
}
return ;
}
poj 3164 Command Network的更多相关文章
- poj 3164 Command Network(最小树形图模板)
Command Network http://poj.org/problem?id=3164 Time Limit: 1000MS Memory Limit: 131072K Total Subm ...
- POJ 3164——Command Network——————【最小树形图、固定根】
Command Network Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 15080 Accepted: 4331 ...
- POJ 3164 Command Network 最小树形图
题目链接: 题目 Command Network Time Limit: 1000MS Memory Limit: 131072K 问题描述 After a long lasting war on w ...
- POJ 3164 Command Network ( 最小树形图 朱刘算法)
题目链接 Description After a long lasting war on words, a war on arms finally breaks out between littlek ...
- POJ 3164 Command Network (最小树形图)
[题目链接]http://poj.org/problem?id=3164 [解题思路]百度百科:最小树形图 ]里面有详细的解释,而Notonlysucess有精简的模板,下文有对其模板的一点解释,前提 ...
- POJ 3164 Command Network(最小树形图模板题+详解)
http://poj.org/problem?id=3164 题意: 求最小树形图. 思路: 套模板. 引用一下来自大神博客的讲解:http://www.cnblogs.com/acjiumeng/p ...
- poj 3164 Command Network (朱刘算法)
题目链接: http://poj.org/problem?id=3164 题目大意: 有n个点(用坐标表示)各点编号分别为1—>n,m条单向路,问能否存在一个花费价值最小的网络,能使从1点到达任 ...
- POJ 3164 Command Network 最小树形图模板
最小树形图求的是有向图的最小生成树,跟无向图求最小生成树有很大的区别. 步骤大致如下: 1.求除了根节点以外每个节点的最小入边,记录前驱 2.判断除了根节点,是否每个节点都有入边,如果存在没有入边的点 ...
- POJ 3164 Command Network 最小树形图 朱刘算法
=============== 分割线之下摘自Sasuke_SCUT的blog============= 最 小树形图,就是给有向带权图中指定一个特殊的点root,求一棵以root为根的有向生成树T, ...
随机推荐
- Oracle case用法
1:update 时做检查使用update mw_contract set payTimes=( case else payTimes end )'; 2:select时使用 select case ...
- spoj 104 Highways (最小生成树计数)
题目链接:http://www.spoj.pl/problems/HIGH/ 题意:求最小生成树个数. #include<algorithm> #include<cstdio> ...
- 【转】Kconfig,Makefile 和 .config
原文网址:http://blog.csdn.net/nxh_love/article/details/11846861 最新在做Sensor驱动移植的时候,发现了Android driver 中有Kc ...
- PHP里关于时间日期大小写(Y,y,M,m...)
y代表年份,取后两位 Y代表年份全部 m代表月份 M代表月份英文简写 d代表天 D代表星期几的简写 h代表小时,12 ...
- ListView之ArrayAdapter
ArrayAdapter 普通的显示listView子项,安卓的内置对象 使用方法: /* ListView :列表 通常有两个职责: a.将数据填充到布局 b.处理点击事件 一个ListView创建 ...
- Guava源码分析——ServiceManager
ServiceManager类: 用于监控服务集的管理器,该类提供了诸如startAsync.stopAsync.servicesByState方法来运行.结束和检查服务集,而且,通过监听器 ...
- [RxJS] Using Observable.create for fine-grained control
Sometimes, the helper methods that RxJS ships with such as fromEvent, fromPromise etc don't always p ...
- Android进程间通信(IPC)机制Binder简要介绍和学习计划
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6618363 在Android系统中,每一个应用 ...
- C++入门学习——标准模板库之vector
vector(向量容器),是 C++ 中十分实用一个容器.vector 之所以被觉得是一个容器,是由于它可以像容器一样存放各种类型的对象,简单地说,vector 是一个可以存放随意类型(类型可以是in ...
- javascript 阻止多次点击造成的轮播混乱
function nextSlider(){ //使用b作为开关,只有动画完成后才能进行下一次运动 if(b){ //如果b为真,则马上设置b为false,如果startmove的回调没有重新设置b的 ...