Uva11183-Teen Girl Squad(有向图最小生成树朱刘算法)
解析: 裸的有向图最小生成树
代码
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iterator>
#include<stack>
using namespace std;
const int INF=1e9+;
const int eps=0.0000001;
const int maxn=;
struct edge
{
int u,v,w;
edge(int u=,int v=,int w=):u(u),v(v),w(w){}
}E[];
int pre[maxn],InEdge[maxn],vis[maxn],id[maxn];
int Dir_MST(int root,int Vcnt,int Ecnt)
{
int ret=;
while(true)
{
for(int i=;i<=Vcnt;i++) InEdge[i]=INF;
for(int i=;i<=Ecnt;i++)
{
edge& e=E[i];
int u=e.u,v=e.v,w=e.w;
if(u==v) continue;
if(w<InEdge[v]) { InEdge[v]=w; pre[v]=u; } //找最小的指向v的边
}
InEdge[root]=;
for(int i=;i<=Vcnt;i++) if(i!=root&&InEdge[i]==INF) return -;//存在某个点跟整个图分离
int ID=;
for(int i=;i<=Vcnt;i++) vis[i]=id[i]=-;
for(int i=;i<=Vcnt;i++)
{
ret+=InEdge[i]; //把那些边加进答案
int a=i;
while(vis[a]!=i&&id[a]==-&&a!=root){ vis[a]=i; a=pre[a]; }
if(id[a]==-&&a!=root)
{
++ID;
for(int b=pre[a];b!=a;b=pre[b]) id[b]=ID; //重新编号
id[a]=ID;
}
}
if(ID==) return ret; //找到解
for(int i=;i<=Vcnt;i++) if(id[i]==-) id[i]=++ID; //独立的点编号
for(int i=;i<=Ecnt;i++)
{
edge& e=E[i];
int u=e.u,v=e.v;
e.u=id[u];
e.v=id[v];
if(id[u]!=id[v]) e.w-=InEdge[v]; //之前加的那一部分要减掉
}
Vcnt=ID;
root=id[root];
}
}
int main()
{
int T,Case=;
scanf("%d",&T);
while(T--)
{
int N,M,u,v,w;
scanf("%d%d",&N,&M);
for(int i=;i<=M;i++)
{
scanf("%d%d%d",&u,&v,&w);
u++; v++;
E[i]=edge(u,v,w);
}
int ans=Dir_MST(,N,M);
printf("Case #%d: ",++Case);
if(ans==-) printf("Possums!\n");
else printf("%d\n",ans);
}
return ;
}
Uva11183-Teen Girl Squad(有向图最小生成树朱刘算法)的更多相关文章
- 训练指南 UVA- 11865(有向最小生成树 + 朱刘算法 + 二分)
layout: post title: 训练指南 UVA- 11865(有向最小生成树 + 朱刘算法 + 二分) author: "luowentaoaa" catalog: tr ...
- UVa11183 Teen Girl Squad, 最小树形图,朱刘算法
Teen Girl Squad Input: Standard Input Output: Standard Output You are part of a group of n teenage ...
- UVA-11183 Teen Girl Squad (最小树形图、朱刘算法模板)
题目大意:给一张无向图,求出最小树形图. 题目分析:套朱-刘算法模板就行了... 代码如下: # include<iostream> # include<cstdio> # i ...
- UVA:11183:Teen Girl Squad (有向图的最小生成树)
Teen Girl Squad Description: You are part of a group of n teenage girls armed with cellphones. You h ...
- 朱刘算法 有向图定根的最小生成树poj3164
关于为什么不能用Prim求解此类问题,如下 Prim可以看成是维护两个顶点集或者看成维护一颗不断生成的树(感觉前一种说法好一点) 倘若是有向图有三个顶点1.2.3 边的情况如下 1->2: ...
- 最小树形图——朱刘算法(Edmonds)
定义:一个有向图,存在从某个点为根的,可以到达所有点的一个最小生成树,则它就是最小树形图. 朱刘算法实现过程: [在选出入边集后(看步骤1),若有向图中不存在有向环,说明该图就是最小树形图] 1,选入 ...
- 最小树形图--朱刘算法([JSOI2008]小店购物)
题面 luogu Sol 首先设一个 \(0\) 号点,向所有点连边,表示初始价值 显然这个图的一个 \(0\) 为根的最小有向生成树的边权和就是每个买一次的最小价值 再买就一定能优惠(包含 \(0\ ...
- poj3164(最小树形图&朱刘算法模板)
题目链接:http://poj.org/problem?id=3164 题意:第一行为n, m,接下来n行为n个点的二维坐标, 再接下来m行每行输入两个数u, v,表点u到点v是单向可达的,求这个有向 ...
- POJ--3164--Command Network【朱刘算法】最小树形图
链接:http://poj.org/problem?id=3164 题意:告诉n个点坐标,m条边表示两个点之间有路.从1点開始建立一个有向图最小生成树. 朱刘算法模板题 =============== ...
随机推荐
- zoj3802:easy 2048 again(状压dp)
zoj月赛的题目,非常不错的一个状压dp.. 题目大意是一个一维的2048游戏 只要有相邻的相同就会合并,合并之后会有奖励分数,总共n个,每个都可以取或者不取 问最终得到的最大值 数据范围n<= ...
- WPF - ViewModle中关闭Window
在Binding close event时候,需要从ViewModel关闭Window. 一个很简洁的解决方案就是,将Window 当做CommandParameter传过去. Command=&qu ...
- C# - List操作 - 按照字母排序
有Family的类如下: public class FamilyModel { public string Name { set; get; } } 创建List List<FamilyMode ...
- pyqt一个小例子
# -*- coding: utf-8 -*- __author__ = 'Administrator' from PyQt4 import Qt,QtCore,QtGui import sys,ra ...
- [RxJS] Reactive Programming - New requests from refresh clicks -- merge()
Now we want each time we click refresh button, we will get new group of users. So we need to get the ...
- React 入门最好的实例-TodoList
React 的核心思想是:封装组件,各个组件维护自己的状态和 UI,当状态变更,自动重新渲染整个组件. 最近前端界闹的沸沸扬扬的技术当属react了,加上项目需要等等原因,自己也决定花些时间来好好认识 ...
- CodeManage 源代码管理器v2.0发布
下载地址 欢迎大家提出宝贵的意见和bug
- HTML与CSS入门——第七章 使用表格显示信息
知识点: 1.创建简单表格的方法 2.控制表格大小的方法 3.对齐内容及在表格中跨越行和列的方法 7.1 创建简单的表格: table标签,border控制边框 tr标签,创建表格的行,包含td td ...
- javascript 模仿 html5 placeholder
<form action="?action=deliver" method="post" class="deliver-form"&g ...
- AngularJs练习Demo5
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...