训练指南 UVA- 11865(有向最小生成树 + 朱刘算法 + 二分)
layout: post
title: 训练指南 UVA- 11865(有向最小生成树 + 朱刘算法 + 二分)
author: "luowentaoaa"
catalog: true
mathjax: true
tags:
- 最小生成树
- 图论
- 训练指南
Stream My Contest
二分带宽,然后判断最小生成树是否小于cost值。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=100+50;
const ll inf=1000000000;;
/// 固定根的最小树型图,邻接矩阵写法
struct MDST{
int n;
int w[maxn][maxn]; ///边权
int vis[maxn]; ///访问标记,仅用来判断无解
int ans; ///计算答案
int removed[maxn]; ///每个点是否被删除
int cid[maxn]; ///所在圈编号
int pre[maxn]; ///最小入边的起点
int iw[maxn]; ///最小入边的权值
int max_cid; ///最大圈编号
void init(int n){
this->n=n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)w[i][j]=inf;
}
void AddEdge(int u,int v,int cost){
w[u][v]=min(w[u][v],cost); ///重边取权值最小的
}
///从s出发能到达多少个结点
int dfs(int s){
int ans=1;
vis[s]=1;
for(int i=0;i<n;i++)if(!vis[i]&&w[s][i]<inf)ans+=dfs(i);
return ans;
}
///从u出发沿着pre指针找圈
bool cycle(int u){
max_cid++;
int v=u;
while(cid[v]!=max_cid){cid[v]=max_cid;v=pre[v];}
return v==u;
}
/// 计算u的最小入弧,入弧起点不得在圈c中
void update(int u){
iw[u]=inf;
for(int i=0;i<n;i++)
if(!removed[i]&&w[i][u]<iw[u]){
iw[u]=w[i][u];
pre[u]=i;
}
}
///根节点为s,如果失败返回false
bool solve(int s){
memset(vis,0,sizeof(vis));
if(dfs(s)!=n)return false;
memset(removed,0,sizeof(removed));
memset(cid,0,sizeof(cid));
for(int u=0;u<n;u++)update(u);
pre[s]=s;iw[s]=0; /// 根结点特殊处理
ans=max_cid=0;
for(;;){
bool have_cycle=false;
for(int u=0;u<n;u++)if(u!=s&&!removed[u]&&cycle(u)){
have_cycle=true;
/// 以下代码缩圈,圈上除了u之外的结点均删除
int v=u;
do{
if(v!=u)removed[v]=1;
ans+=iw[v];
/// 对于圈外点i,把边i->v改成i->u(并调整权值);v->i改为u->i
/// 注意圈上可能还有一个v'使得i->v'或者v'->i存在,因此只保留权值最小的i->u和u->i
for(int i=0;i<n;i++)if(cid[i]!=cid[u]&&!removed[i]){
if(w[i][v]<inf)w[i][u]=min(w[i][u],w[i][v]-iw[v]);
w[u][i]=min(w[u][i],w[v][i]);
if(pre[i]==v)pre[i]=u;
}
v=pre[v];
}while(v!=u);
update(u);
break;
}
if(!have_cycle)break;
}
for(int i=0;i<n;i++)
if(!removed[i])ans+=iw[i];
return true;
}
};
MDST solver;
struct Edge{
int u,v,b,c;
bool operator <(const Edge& rhs)const{
return b>rhs.b;
}
};
const int maxm=10000+10;
int n,m,C;
Edge edges[maxm];
bool check(int cnt){
solver.init(n);
for(int i=0;i<cnt;i++)
solver.AddEdge(edges[i].u,edges[i].v,edges[i].c);
if(!solver.solve(0))return false;
return solver.ans<=C;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int t;
/* int a=inf;
cout<<a<<endl<<1000000000<<endl;*/
cin>>t;
while(t--){
cin>>n>>m>>C;
for(int i=0;i<m;i++)cin>>edges[i].u>>edges[i].v>>edges[i].b>>edges[i].c;
sort(edges,edges+m);
int l=1,r=m,ans=-1;
while(l<=r){
int mid=(l+r)/2;
if(check(mid)){ans=edges[mid-1].b;r=mid-1;}
else l=mid+1;
}
if(ans<0)cout<<"streaming not possible."<<endl;
else cout<<ans<<" kbps"<<endl;
}
return 0;
}
训练指南 UVA- 11865(有向最小生成树 + 朱刘算法 + 二分)的更多相关文章
- uva11865 朱刘算法+二分
这题说的需要最多花费cost元来搭建一个比赛网络,网络中有n台机器,编号为0 - n-1其中机器0 为服务器,给了n条线有向的和他们的花费以及带宽 计算,使得n台连接在一起,最大化网络中的最小带宽, ...
- UVA-11865 Stream My Contest (朱-刘 算法+二分)
题目大意:有一张n个顶点,m条边的有向图,根节点为0.每条边有两个权值,一个是费用c,一个是长度b.问在总费用不超过cost的情况下选出若干条边,使得n个点连通时的边的最短长度的最大值是多少. 题目分 ...
- Uva11183-Teen Girl Squad(有向图最小生成树朱刘算法)
解析: 裸的有向图最小生成树 代码 #include<cstdio> #include<cstring> #include<string> #include< ...
- 训练指南 UVA - 11354(最小生成树 + 倍增LCA)
layout: post title: 训练指南 UVA - 11354(最小生成树 + 倍增LCA) author: "luowentaoaa" catalog: true ma ...
- 训练指南 UVA - 11419(二分图最小覆盖数)
layout: post title: 训练指南 UVA - 11419(二分图最小覆盖数) author: "luowentaoaa" catalog: true mathjax ...
- 训练指南 UVA - 11383(KM算法的应用 lx+ly >=w(x,y))
layout: post title: 训练指南 UVA - 11383(KM算法的应用 lx+ly >=w(x,y)) author: "luowentaoaa" cata ...
- 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)
layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...
- 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)
layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...
- 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)
layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...
随机推荐
- 【BZOJ 2324】[ZJOI2011]营救皮卡丘 费用流
本人实行诱骗拐卖(利用自然分层与实际意义),正解拼接补充(充分利用最大流限制(不浪费任何一个走出去的机会而不是不浪费任何一个已有的流)与问题转换) #include <cstdio> #i ...
- HttpClientUntils工具类的使用测试及注意事项(包括我改进的工具类和Controller端的注意事项【附 Json 工具类】)
HttpClient工具类(我改过): package com.taotao.httpclient; import java.io.IOException; import java.net.URI; ...
- Clevo P950笔记本加装4G模块
要补全的电路部分如下(原理图见附件) 这里经过尝试,发现左上角R217,R218不用接,3G_POWER部分不接(包括MTS3572G6.UK3018及电阻电容,3G_PWR_EN实测是3.3V,驱动 ...
- 安卓topbar编码实战
1.先在res->value下新建attrs.xml文件 <?xml version="1.0" encoding="utf-8"?> < ...
- nodejs npm insttall 带不带-g这个参数的区别
-g 中的g是global的意思所以带-g这个参数是全局安装,不带-g这个参数是本地安装. 在windows系统中全局安装的目录在:C:\Users\linsenq\AppData\Roaming\n ...
- 学习python类
类:Python 类提供了面向对象编程的所有基本特征: 允许多继承的类继承机制, 派生类可以重写它父类的任何方法, 一个方法可以调用父类中重名的方法. 对象可以包含任意数量和类型的数据成员. 作为模块 ...
- 开发中常遇到的Python陷阱和注意点
最近使用Python的过程中遇到了一些坑,例如用datetime.datetime.now()这个可变对象作为函数的默认参数,模块循环依赖等等. 在此记录一下,方便以后查询和补充. 避免可变对象作为默 ...
- python异常之with
1.基本语法 with expression [as target]: with_body 参数说明: expression:是一个需要执行的表达式: target:是一个变量或者元组,存储的是exp ...
- idea使用(一)
基本上正式开发的常用工具基本都集成了,而且基本都在你非常容易触到的位置.说说我比较常用的: 1.ant 你懂的 2.maven你也懂的 3.SVN相比之下,IDEA的SVN的提交提供了更多的选项和功能 ...
- DOM常用对象
一.select对象 HEML中的下拉列表 属性: 1.options 获得当前select下所有option 2.options[i] 获得当前select下i位置的option 3.selecte ...