Codeforces 147 B. Smile House
题目链接:http://codeforces.com/contest/147/problem/B
求有向图的最小正权环的大小 ${n<=300}$
非常显然的有${n^{3}log^2}$的做法,令${f_{(s,i,j)}}$表示从第${i}$个点走到第${j}$个点走了${2^{s}}$步的最大权值,然后二分答案,每次利用$f$数组计算答案。
问题在于算一下时间感觉是不太对的...然而居然跑过去了 QwQ
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 310
#define llg int
#define inf 0x3f3f3f3f
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,a[][maxn][maxn],dp[][maxn][maxn],up; inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} void init()
{
cin>>n>>m;
up=(llg)floor(log2((long double)n));
for (llg i=;i<=up;i++) for (llg j=;j<=n;j++) for (llg k=;k<=n;k++) dp[i][j][k]=-*inf;
for (llg s=;s<=up;s++) for (llg i=;i<=n;i++) dp[s][i][i]=;
for (llg i=;i<=m;i++)
{
llg u=getint(),v=getint();
dp[][u][v]=getint(),dp[][v][u]=getint();
}
} void make_dp()
{
for (llg s=;s<=up;s++)
for (llg k=;k<=n;k++)
for (llg i=;i<=n;i++)
{
if(dp[s - ][i][k]==-inf) continue;
for (llg j=;j<=n;j++)
dp[s][i][j]=max(dp[s][i][j],dp[s-][i][k]+dp[s-][k][j]);
}
} bool check(llg x)
{
for (llg i=;i<=up;i++) for (llg j=;j<=n;j++) for (llg k=;k<=n;k++) a[i][j][k]=-*inf;
for (llg i=;i<=n;i++) a[][i][i]=;
llg p=;
for (llg s=;s<=up;s++)
{
if (((x>>s)&)==) continue;
p++;
for (llg i=;i<=n;i++) a[p][i][i]=;
for (llg k=;k<=n;k++)
for (llg i=;i<=n;i++)
for (llg j=;j<=n;j++)
a[p][i][j]=max(a[p][i][j],a[p-][i][k]+dp[s][k][j]);
}
for (llg i=;i<=n;i++) if (a[p][i][i]>) return ;
return ;
} int main()
{
yyj("a");
init();
make_dp();
llg l=,r=n+,ans=;
while (l<=r)
{
llg mid=(l+r)>>;
if (check(mid)) r=mid-,ans=mid;else l=mid+;
}
if (l>=n+) ans=;
cout<<ans;
return ;
}
Codeforces 147 B. Smile House的更多相关文章
- Codeforces 147B Smile House(DP预处理 + 倍增)
题目链接 Smile House 题意 给定一个$n$个点的有向图,求一个点数最少的环,使得边权之和$>0$,这里的环可以重复经过点和边. 满足 $n <= 300$ 首先答案肯 ...
- Codeforces Round #147 (Div. 2)
A. Free Cash 判断值相同的最长长度. B. Young Table 按从上到下,从左到右排序,每个位置最多交换一次. C. Primes on Interval \(p_i\)表示位置\( ...
- 13年7月13日CF练习 Codeforces Round #147 (Div. 2)
这场div2可以说是我见过的比较水的一场吧.基本都是一眼题. 比赛地址http://acm.bnu.edu.cn/bnuoj/contest_show.php?cid=1836 题号是237A-237 ...
- CodeForces - 404B(模拟题)
Marathon Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Sta ...
- Codeforces Round #237 (Div. 2) B题模拟题
链接:http://codeforces.com/contest/404/problem/B B. Marathon time limit per test 1 second memory limit ...
- Codeforces GYM 100114 C. Sequence 打表
C. Sequence Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description ...
- codeforces 691F 暴力
传送门:https://codeforces.com/contest/691/problem/F 题意:给你n个数和q次询问,每次询问问你有多少对ai,aj满足ai*aj>=q[i],注意 a* ...
- codeforces 691E 矩阵快速幂+dp
传送门:https://codeforces.com/contest/691/problem/E 题意:给定长度为n的序列,从序列中选择k个数(可以重复选择),使得得到的排列满足xi与xi+1异或的二 ...
- Educational Codeforces Round 8 F. Bear and Fair Set 最大流
F. Bear and Fair Set 题目连接: http://www.codeforces.com/contest/628/problem/F Description Limak is a gr ...
随机推荐
- PHP(一般标签介绍,标签特性,实体名称,绝对路径与相对路径)
h1:为标题 h1~h6 标题会逐渐变小 需更换标签里面的数字 如: <h1>这是标题123</h1>---标题 <h2>这是标题123</h2>-- ...
- LIBS入门
样品汽化产生自由原子,原子电子的激发诱导光辐射产生表征原子的分立光谱,采集和分析光辐射. 光源:1064nm Nd:YAG固态激光器,10ns脉冲,焦点光密度1 GW·cm−2 可见和紫外光源. ...
- mysql提权常用方法。 hack某某
一般是root权限,知道mysql root权限,root账号密码 启动项提权:原理:利用高权限的root写入一个vbs脚本到启动项,再通过一些方法如ddos,社工管理员之类的方法来让服务器重启,运行 ...
- java.net.UnknownHostException 异常处理
修改hosts文件: 1.把机器名和ip写在下面 2.hosts文件生效 soure /etc/hosts
- Redis安装[Windows]
一. redis下载地址: https://github.com/ServiceStack/redis-windows/tree/master/downloads 根据需要的下载对应版本*.zip ...
- shell编程之awk命令详解
shell编程之awk命令详解 a:focus { outline: thin dotted #333; outline: 5px auto -webkit-focus-ring-color; out ...
- python之SQLAlchemy组件
介绍 SQLAlchemy 是一个 ORM 框架,可以帮助我们使用面向对象的方式快速实现数据库操作. 组成部分: Engine,框架的引擎 Connection Pooling ,数据库连接池 Dia ...
- 【UML】NO.53.EBook.6.UML.2.001-【Thinking In UML 大象 第二版】- 概述
1.0.0 Summary Tittle:[UML]NO.53.EBook.6.UML.2.001-[Thinking In UML 大象 第二版]- 概述 Style:DesignPattern S ...
- 怎样用Java自制优秀的图片验证码?这样!
Completely Automated Public Turing test to tell Computers and Humans Apart 全自动区分计算机和人类的图灵测试 简称CAPTCH ...
- Unity骨骼动画资源解析与优化
一,背景 最近发现项目的动画文件有点大,不光内存大,而且文件也很大,所以从这2个方面下手处理 二,动画文件大小优化 为了优化动画文件大小,我们可以先分析下文件,Ctrl+D将动画文件从FBX拷贝出来, ...