【题解】cycle
【题解】cycle
题目描述
给定一个无向图,求一个环,使得环内边权\(\div\)环内点数最大。
数据范围
\(n \le 5000\) \(m\le 10000\)
\(Solution\)
考虑到我们可以对答案的式子变一下形,
\(\frac{\Sigma_{i\in V'} w_i}{|V'|}\le ans\)
\(\Sigma_{i\in V'}w_i-ans\times |V'|\le0\)
这一步不要看不懂了(\(i\)共有\(|V'|\)个,所以\(ans\)仍然总共被加了\(|V'|\)次):
\(\Sigma_{i\in V'} (w_i-ans) \le 0\)
即
\(\Sigma_{i \in V'} w'_i\le0\)
直接\(spfa\)跑负环就好了。
实际上这是一个很有用的方法,先根据题目答案的意义判断是否存在答案单调性,再通过数学变换得到我们想要的式子。
或者感性理解,我们得到一堆边的平均边权是\(ans\)了,那么这个边集的所有权减去这个\(ans\)然后加起来一定等于\(0\)。假如加起来比\(0\)大,说明不存在,假设比这个小,说明有更优解。
上好看的代码qvq
#include<bits/stdc++.h>
using namespace std;typedef long long ll;
#define DRP(t,a,b) for(register int t=(a),edd=(b);t>=edd;--t)
#define RP(t,a,b) for(register int t=(a),edd=(b);t<=edd;++t)
#define ERP(t,a) for(register int t=head[a];t;t=e[t].nx)
#define midd register int mid=(l+r)>>1
#define TMP template < class ccf >
TMP inline ccf qr(ccf b){
register char c=getchar();register int q=1;register ccf x=0;
while(c<48||c>57)q=c==45?-1:q,c=getchar();
while(c>=48&&c<=57)x=x*10+c-48,c=getchar();
return q==-1?-x:x;}
TMP inline ccf Max(ccf a,ccf b){return a<b?b:a;}
TMP inline ccf Min(ccf a,ccf b){return a<b?a:b;}
TMP inline ccf Max(ccf a,ccf b,ccf c){return Max(a,Max(b,c));}
TMP inline ccf Min(ccf a,ccf b,ccf c){return Min(a,Min(b,c));}
TMP inline ccf READ(ccf* _arr,int _n){RP(t,1,_n)_arr[t]=qr((ccf)1);}
//----------------------template&IO---------------------------
const int maxn=3e3+15;
const int maxm=1e4+15;
struct E{
int to,nx;
long double w;
}e[maxm];
const long double EPS=1e-10;
int head[maxn];
int usd[maxn];
bool in[maxn];
double d[maxn];
double sa[maxm];
int cnt;
int n,m;
inline void add(int fr,int to,long double w){
cnt++;
e[cnt].to=to;
e[cnt].nx=head[fr];
e[cnt].w=w;
sa[cnt]=w;
head[fr]=cnt;
}
bool c=0;
void spfa(int now){
if(c) return;
usd[now]=1;
ERP(t,now){
if(d[e[t].to]>d[now]+e[t].w){
d[e[t].to]=d[now]+e[t].w;
if(c||usd[e[t].to]) return void(c=1);
spfa(e[t].to);
}
}
usd[now]=0;
}
inline bool chek(long double x){
RP(t,1,m) e[t].w=sa[t]-x;
RP(t,1,n) usd[t]=d[t]=0;c=0;
RP(t,1,n) if(!usd[t]) spfa(t);
return c;
}
int main(){
n=qr(1);m=qr(1);
int t1,t2;
long double t3;
long double l=-1e7-(long double)1,r=1e7+(long double)1;
long double mid;
RP(t,1,m){
t1=qr(1);t2=qr(1);
scanf("%Lf",&t3);
add(t1,t2,t3);
}
do{
mid=(l+r)/(long double)2;
if(chek(mid))
r=mid;
else
l=mid;
}while(l+EPS<r);
printf("%.8Lf\n",l);
return 0;
}
/*
dfs序+树连剖腹
乱做
orz yyb
不行 有情况没有考虑到!
直接tarjin
二分就好了吧 乱做OK
不行 我是****
*/
【题解】cycle的更多相关文章
- 【题解】【链表】【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode 题解]: Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【题解】Shortest Cycle
原题链接:CF1205B 题目大意 给定\(n\)个整数\(a_1,a_2,a_3, \dots ,a_n\),若\(i \neq j\)且\(a_i \land a_j \neq 0\),则 ...
- [LeetCode]题解(python):142-Linked List Cycle II
题目来源: https://leetcode.com/problems/linked-list-cycle-ii/ 题意分析: 给定一个链表,如果链表有环,返回环的起始位置,否则返回NULL.要求常量 ...
- [LeetCode]题解(python):141-Linked List Cycle
题目来源: https://leetcode.com/problems/linked-list-cycle/ 题意分析: 给定一个链表,判断链表是否有环.要求O(1)空间时间复杂度. 题目思路: 用快 ...
- UVALive 7501 Business Cycle(二分)题解
题意:n个数,有一个起始值,按顺序从第一个开始不断循环取数,如果取完后相加小于0就变为0,最多取p个数,问你得到大于等于值g所需要的最小起始值为多少 思路:这题目爆long long爆的毫无准备,到处 ...
- LeetCode 题解之Linked List Cycle II
1.题目描述 2.问题分析 使用快慢指针方法判断链表是否有环,然后寻找环开始的节点. 3.代码 ListNode *detectCycle(ListNode *head) { if( head == ...
- LeetCode题解之Linked List Cycle
1.题目描述 2.问题分析 使用快慢指针方法,一个快指针,一个慢指针,如果到某个时候,快指针追上了慢指针,则说明有环存在. 3.代码 bool hasCycle(ListNode *head) { i ...
- PAT甲题题解-1122. Hamiltonian Cycle (25)-判断路径是否是哈密顿回路
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789799.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- [TJOI2016][HEOI2016]排序
题目大意: 给定一个$1\sim n(n\leq10^5)$的全排列,有$m(m\leq10^5)$次操作,每次把区间$[l,r]$按照升序或降序排序.最后询问所有操作完成后,位置为$q$的数是多少. ...
- Theam,style
Theam <!-- Base application theme. --> <!--<style name="AppTheme" parent=" ...
- SecureCRT导出服务器列表或配置文件
说明:SecureCRT没有Xshell那么简单有直接导出的功能,但是可以通过技巧的方式来操作. 1.打开SecureCRT,点击菜单栏的[Opitions]->[Global Opitions ...
- Windbg调试Sql Server 进程
http://blog.csdn.net/bcbobo21cn/article/details/52261466 http://www.sqlservercentral.com/blogs/asche ...
- HTTP协议header头域
HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP协议的详细内 容请参考RFC2616.HTTP协议采用了请求/响应模型.客 ...
- Scut游戏服务器引擎5.6.3.5发布
版本:5.6.3.5 (2013-11-25) 1. 优化实体ChangeKey队列,减少写库IO(默认为5分钟写入一次数据库) 2. 优化Protobuf序列化启用自动GZip压缩,减少Redis内 ...
- PHP 5.4 中经 htmlspecialchars 转义后的中文字符串为空的问题
PHP 5.4.3 环境中测试了一个在 PHP 5.2 环境下运行正常的程序,却发现本应正常提交一个中文字符串到数据库的代码却提交了一个空字符串,经过排查,该字符串在经 htmlspecialchar ...
- JSON API:用 JSON 构建 API 的标准指南中文版
译文地址:https://github.com/justjavac/json-api-zh_CN 假设你和你的团队以前争论过使用什么方式构建合理 JSON 响应格式, 那么 JSON API 就是你的 ...
- ylb:转换函数Cast,Convert 指定格式返回
ylbtech-SQL Server:SQL Server-转换函数Cast,Convert 指定格式返回 转换函数Cast,Convert 指定格式返回. ylb:转换函数Cast,Convert ...
- OSG+VS2010+win7环境搭建 (转)
OSG+VS2010+win7环境搭建 Win7下 osg+vs2010环境搭建 一.相关准备 a) Osg源码 当前最新版:OpenSceneGraph的3.0.0.zip 下载链接: http:/ ...