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 ...
随机推荐
- 主流框架的搭建(VUE,React)
vue脚手架:cnpm install vue vue-cli -gvue init webpack/webpack-simple shuaige(新建文件夹的名字)cd shuaigecnpm in ...
- eclipse连接mysql数据库实现怎删改查操作实例(附带源码)
package model; public class User { private int id; private String name; private String password; pub ...
- Java应用之shiro
Apache Shiro是一个强大而灵活的开源安全框架,它能够干净利落地处理身份认证,授权,企业会话管理和加密. 以下是你可以用 Apache Shiro所做的事情: 1.验证用户 2. 对用户执行访 ...
- [No0000D1]WPF—TreeView无限极绑定集合形成树结构
1.如图所示:绑定树效果图 2.前台Xaml代码: <Window x:Class="WpfTest.MainWindow" xmlns="http://schem ...
- Codeforces Round div2 #541 题解
codeforces Round #541 abstract: I构造题可能代码简单证明很难 II拓扑排序 III并查集 启发式排序,带链表 IV dp 处理字符串递推问题 V 数据结构巧用:于二叉树 ...
- J2EE [web] 403.500.404页面配置
如果想让系统在出错后,看到自定义的错误提示页面,而不是满屏错误原因以及代码. 1.web.xml中 <error-page> <error-code>403</error ...
- Spring Boot 配置_yaml语法介绍 day02
一.Spring Boot 的全局配置文件(application.properties.application.yml) 1.这两种配置文件是SpringBoot 支持的对默认配置修改的格式.命名和 ...
- python框架之Django(13)-admin组件
使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INSTALLED_APPS ...
- 3.1.1 Spring 简介
1. 概念 Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架. IoC : Inversion of Control AOP : Aspect Oriented Program ...
- pprint
pprint = pretty printer 经常用来打印 字典.json 打印出的格式会是较为标准的格式 目的:方便调试,查看中间结果,因为觉得设断点调试相对麻烦. [运行环境:macOS 10. ...