题目
D Link with Game Glitch
Link is developing a game. In this game, players can craft things using various types of resources, and things crafted can also be used to craft other things.
Formally speaking, there are nn types of items, numbered from 11 to nn, and mm recipes in the game. In the ii-th recipe, players can use ka_ik∗ai items of the b_ibi-th type to craft kc_ik∗ci items of the d_idi-th type, where kk can be any positive real number.
One day, he finds that one player owns more than 18,446,744,073,709,551,615 identical items, which causes a server crash. This is obviously impossible without using glitches.
Link soon finds out that there is something wrong with the crafting recipe. Players may get infinite resources by crafting some special things over and over again!
Link doesn’t want to adjust the recipes one by one, so he simply added an argument ww. Now players can use ka_ik∗ai items of the b_ibi-th type to craft wk*c_iw∗k∗ci items of the d_idi-th type.
Link wonders: What’s the maximum ww that he can set so that no player can get infinite items by crafting things over and over again?
在游戏中有n中物品和m种兑换规则,你可以用 k ∗ a i 个 b i 去兑换 k ∗ c i 个 d i k*a_i个b_i去兑换k*c_i个d_i k∗ai个bi去兑换k∗ci个di,k不一定是整数
但是这个规则出现了问题,即图中出现了边权积大于1的环
这里我们可以用二分求得答案
由于边权积过大,long long肯定是不够的,我们可以把边权转化为对数,再用bellman_ford算法(有负权边)求最短路,如果出现大于0的距离,即 l o g x > 0 logx>0 logx>0那么则有x大于1,说明二分的该答案有问题,继续向下二分得到结果
tags:二分 最短路(Bellman_Ford)
AC代码
/****************
*@description:for the Escape Project
*@author: Nebula_xuan
* @Date: 2022-07-26 11:22:44
*************************************************************************/
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10, M = 2e3 + 10;
const double eps = 1e-9;
long long n, m, a[M], b[M], c[M], d[M];
double dist[N], w[M];
int Bellman_Ford()
{
for (int i = 1; i <= n; i++)
dist[i] = 0.0;
bool flag = false;
for (int i = 1; i < n; i++)
{
flag = false;
for (int j = 1; j <= m; j++)
{
if (dist[b[j]] + w[j] < dist[d[j]])
{
flag = true;
dist[d[j]] = dist[b[j]] + w[j];
}
}
if (!flag)
return false;
}
return true;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> a[i] >> b[i] >> c[i] >> d[i];
double l = 0.0, r = 1.0;
while (r - l > eps)
{
double mid = (l + r) / 2.0;
for (int i = 1; i <= m; i++)
w[i] = -log(1.0 * c[i] * mid / a[i]);
if (!Bellman_Ford())
l = mid;
else
r = mid;
}
printf("%.10lf\n",l);
}
K Link with Bracket Sequence I
Link has a bracket sequence aa of length nn, which is a subsequence of a valid bracket sequence bb of length mm.
Link doesn’t remember bb, so he wonders the number of possible sequences bb.
A bracket sequence is valid if it satisfies any of the following conditions:
-
Its length is 00.
-
It can be represented as (A)(A), where AA is a valid bracket sequences.
-
It can be represented as ABAB, where AA and BB are both valid bracket sequence.
A sequence aa is a subsequence of a sequence bb if aa can be obtained from bb by deletion of several (possibly, zero or all) elements.
给你一个长度为n的括号序列,问你长度为m的原序列(且合法)的情况有多少种
这里我们用DP进行考虑,有 f [ i ] [ j ] [ k ] f[i][j][k] f[i][j][k],其中i表示原序列的长度
,j表示匹配的长度,k表示左括号比右括号多k的时的方案数
当我们选择添加一个左括号时,可以随意添加
但是如果我们添加的是一个右括号,当且仅当左括号数量大于右括号数量的时候匹配才成立
tags:DP
AC代码
/****************
*@description:for the Escape Project
*@author: Nebula_xuan
* @Date: 2022-07-26 10:48:24
*************************************************************************/
#include <bits/stdc++.h>
using namespace std;
const