【题解】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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- SecureCRT保持连接,不会过一段时间关闭Session
[Options]->[Global Options]->[General]->[Default Session]点击[Edit default settings]按钮,在[Term ...
- iphone开发-SQLite数据库使用
我现在要使用SQLite3.0创建一个数据库,然后在数据库中创建一个表格. 首先要引入SQLite3.0的lib库.然后包含头文件#import <sqlite3.h> [1]打开数据库, ...
- mac 下mysql常用命令
是那种单独安装的mysql 启动: /usr/local/mysql/bin/mysql -u root -p
- Windows API 教程(七) hook 钩子监听
茵蒂克丝 如何创建一个窗口 手动创建窗口的流程 实际代码 安装钩子 (Install hook) 钩子简介 SetWindowsHookEx 函数 设置监听[键盘]消息 设置监听[鼠标]消息 如何创建 ...
- 黑苹果+win10双系统折腾笔记
寒假趁机在家折腾一下黑苹果 笔记本配置:神船K610D I7 4600 ,其他配置思路一样,驱动要自己找 镜像和工具:OS X Yosemite 10.10.3 镜像 WIN10 TLSB 2016 ...
- Android之——流量管理程序演示样例
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47680811 眼下.市面上有非常多管理手机流量的软件,能够让用户实时获取到自己手机 ...
- 《linux 内核全然剖析》 mktime.c
tm结构体的定义在time.h里面 struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_y ...
- 日历插件js,jquery
常用的日历插件 DatePicker My97DatePicker 文章来源:刘俊涛的博客 地址:http://www.cnblogs.com/lovebing 欢迎关注,有问题一起学习欢迎留言. ...
- nginx的优点
Linux.MySQL.PHP这些框架的优点之前已经介绍过,LNMP和LAMP不同的一点就是Web服务器Nginx,那么Nginx相比Apache有什么优点呢? Nginx是一个小巧而高效的Linux ...
- 【Excle数据透视表】如何复制数据透视表
左边创建完数据透视表,右边是复制过去的部分数据透视表---显示数值状态的内容,为什么复制过来的不是数据透视表呢? 解决办法: 全选定数据透视表再进行粘贴复制 步骤一 单击数据透视表任意单元格→分析→操 ...