【题解】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的更多相关文章

  1. 【题解】【链表】【Leetcode】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  2. [LeetCode 题解]: Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  3. 【题解】Shortest Cycle

    原题链接:CF1205B   题目大意   给定\(n\)个整数\(a_1,a_2,a_3, \dots ,a_n\),若\(i \neq j\)且\(a_i \land a_j \neq 0\),则 ...

  4. [LeetCode]题解(python):142-Linked List Cycle II

    题目来源: https://leetcode.com/problems/linked-list-cycle-ii/ 题意分析: 给定一个链表,如果链表有环,返回环的起始位置,否则返回NULL.要求常量 ...

  5. [LeetCode]题解(python):141-Linked List Cycle

    题目来源: https://leetcode.com/problems/linked-list-cycle/ 题意分析: 给定一个链表,判断链表是否有环.要求O(1)空间时间复杂度. 题目思路: 用快 ...

  6. UVALive 7501 Business Cycle(二分)题解

    题意:n个数,有一个起始值,按顺序从第一个开始不断循环取数,如果取完后相加小于0就变为0,最多取p个数,问你得到大于等于值g所需要的最小起始值为多少 思路:这题目爆long long爆的毫无准备,到处 ...

  7. LeetCode 题解之Linked List Cycle II

    1.题目描述 2.问题分析 使用快慢指针方法判断链表是否有环,然后寻找环开始的节点. 3.代码 ListNode *detectCycle(ListNode *head) { if( head == ...

  8. LeetCode题解之Linked List Cycle

    1.题目描述 2.问题分析 使用快慢指针方法,一个快指针,一个慢指针,如果到某个时候,快指针追上了慢指针,则说明有环存在. 3.代码 bool hasCycle(ListNode *head) { i ...

  9. PAT甲题题解-1122. Hamiltonian Cycle (25)-判断路径是否是哈密顿回路

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789799.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. webservice测试窗体只能用于来自本地计算机的请求

    写在前面 在编写好webservice后,发布到iis服务器,你会发现会有这样的异常“测试窗体只能用于来自本地计算机的请求”. 解决方案 在web.config中添加以下代码即可解决问题 <we ...

  2. Python模拟浏览器上传文件脚本(Multipart/form-data格式)

    http协议本身的原始方法不支持multipart/form-data请求,这个请求由原始方法演变而来的. multipart/form-data的基础方法是post,也就是说是由post方法来组合实 ...

  3. dedecms调用文章发布日期

    <span>[field:pubdate function="MyDate('m-d',@me)"/]</span>

  4. 在程序中使用NV 3D Vision 【转】

    http://www.cnblogs.com/gongminmin/archive/2010/11/21/1883392.html 多年前NVIDIA就发布了3D Vision技术,能提供多种立体渲染 ...

  5. oracle12安装软件后安装数据库,然后需要自己配置监听

    oracle12安装软件后安装数据库,然后需要自己配置监听 没想到你是这样的oracle12: 不能同时安装软件和数据库,分别安装之后,\NETWORD\ADMIN\下面竟然没有listener.or ...

  6. 转: 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?

    from: https://github.com/RubyLouvre/agate/issues/8 今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以 ...

  7. iframe 实现网页本页显示

    <el-dialog title="" :visible.sync="dialogVisible"> <iframe src="ht ...

  8. memcpy( )的使用以及迭代器的使用

    memcpy() -- 拷贝内存内容 相关函数: bcopy(), memccpy(), memmove(), strcpy(), strncpy() 表头文件: #include <strin ...

  9. JS函数库Underscore.js

    http://underscorejs.org/ http://www.css88.com/doc/underscore/ http://www.bootcss.com/p/underscore/

  10. VueJS条件语句:v-if、v-else、v-else-if

    HTML:if-else <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...