题目链接: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的更多相关文章

  1. Codeforces 147B Smile House(DP预处理 + 倍增)

    题目链接  Smile House 题意  给定一个$n$个点的有向图,求一个点数最少的环,使得边权之和$>0$,这里的环可以重复经过点和边.   满足  $n <= 300$ 首先答案肯 ...

  2. Codeforces Round #147 (Div. 2)

    A. Free Cash 判断值相同的最长长度. B. Young Table 按从上到下,从左到右排序,每个位置最多交换一次. C. Primes on Interval \(p_i\)表示位置\( ...

  3. 13年7月13日CF练习 Codeforces Round #147 (Div. 2)

    这场div2可以说是我见过的比较水的一场吧.基本都是一眼题. 比赛地址http://acm.bnu.edu.cn/bnuoj/contest_show.php?cid=1836 题号是237A-237 ...

  4. CodeForces - 404B(模拟题)

    Marathon Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Sta ...

  5. Codeforces Round #237 (Div. 2) B题模拟题

    链接:http://codeforces.com/contest/404/problem/B B. Marathon time limit per test 1 second memory limit ...

  6. Codeforces GYM 100114 C. Sequence 打表

    C. Sequence Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description ...

  7. codeforces 691F 暴力

    传送门:https://codeforces.com/contest/691/problem/F 题意:给你n个数和q次询问,每次询问问你有多少对ai,aj满足ai*aj>=q[i],注意 a* ...

  8. codeforces 691E 矩阵快速幂+dp

    传送门:https://codeforces.com/contest/691/problem/E 题意:给定长度为n的序列,从序列中选择k个数(可以重复选择),使得得到的排列满足xi与xi+1异或的二 ...

  9. 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 ...

随机推荐

  1. Java开发岗位面试题归类

    一.Java基础 1. String类为什么是final的. ( 1.由于String类不能被继承,所以就不会被修改,这就避免了因为继承引起的安全隐患: 2.String类在程序中出现的频率比较高,如 ...

  2. UI自动化框架——构建思维

    目的:从Excel中获取列的值,传输到页面 技巧:尽可能的提高方法的重用率 Java包: 1.java.core包 3个类:1)日志(LogEventListener)扩展web driver自带的事 ...

  3. Ubuntu系统实现将Jupyter notebook项目发布到GitHub

    一.准备 Ubuntu16.04.GitHub账户.Git.Jupyter Notebook项目 二.打开Terminal(用户和邮箱即为你的GitHub注册的账号和邮箱) git config -- ...

  4. python编写shell脚本详细讲解

    python编写shell脚本详细讲解 那,python可以做shell脚本吗? 首先介绍一个函数: os.system(command) 这个函数可以调用shell运行命令行command并且返回它 ...

  5. nginx安装lua模块实现高并发

    nginx安装lua扩展模块 1.下载安装LuaJIT-2.0.4.tar.gz wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz tar ...

  6. 467A

    #include <stdio.h> int main() { int n; int p, q; int rooms=0; scanf("%d", &n); i ...

  7. Centos使用natapp教程

    官网:https://natapp.cn/ 首先在Natapp站注册账号 点击注册 登录后,点击左边 购买隧道,免费/付费均可 根据需要选择隧道协议,这里以web演示,购买隧道 在 https://n ...

  8. IP-v4&IP-v6

    IPv6与IPv4区别: 1:IPv6的地址空间更大.IPv4中规定IP地址长度为32,即有2^32-1个地址: 而IPv6中IP地址的长度为128,即有2^128-1个地址. 2.IPv6的路由表更 ...

  9. Hadoop 集群的三种方式

    1,Local(Standalone) Mode 单机模式 $ mkdir input $ cp etc/hadoop/*.xml input $ bin/hadoop jar share/hadoo ...

  10. 创建多线程的第一种方式——创建Thread子类和重写run方法

    创建多线程的第一种方式——创建Thread子类和重写run方法: 第二种方式——实现Runnable接口,实现类传参给父类Thread类构造方法创建线程: 第一种方式创建Thread子类和重写run方 ...