一道\(0/1\)分数规划+负环

POJ原题链接

洛谷原题链接

显然是\(0/1\)分数规划问题。

二分答案,设二分值为\(mid\)。

然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\(z=(x,y)\),\(time\)为原边权,\(fun\)为原点权,则将该边权换成\(mid\times time[z]+fun[x]\),然后在上面跑\(SPFA\)。

如果有一个环使得\(\sum\{mid\times time[z]+fun[x]\}<0\),则说明\(mid\)小了,而式子表示的意义就是原图存在负环;如果有环使得该式\(\geqslant0\),那么说明答案不超过\(mid\),这时\(SPFA\)就会正常结束。

#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1010;
const int M = 5010;
int fi[N], di[M], ne[M], da[M], fun[N], q[N << 10], cnt[N], l, n;
bool v[N];
double dis[N];
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c<'0' || c>'9'; c = getchar())
p |= c == '-';
for (; c >= '0'&&c <= '9'; c = getchar())
x = x * 10 + (c - '0');
return p ? -x : x;
}
inline void add(int x, int y, int z)
{
di[++l] = y;
da[l] = z;
ne[l] = fi[x];
fi[x] = l;
}
bool judge(double mid)
{
int head = 0, tail = 1, i, x, y;
double z;
bool p = 1;
memset(dis, 66, sizeof(dis));
memset(v, 0, sizeof(v));
memset(cnt, 0, sizeof(cnt));
dis[1] = 0;
q[1] = 1;
while (head != tail && p)
{
x = q[++head];
v[x] = 0;
for (i = fi[x]; i; i = ne[i])
{
y = di[i];
z = mid * da[i] - fun[x];
if (dis[y] > dis[x] + z)
{
dis[y] = dis[x] + z;
cnt[y] = cnt[x] + 1;
if (cnt[y] >= n)
{
p = 0;
break;
}
if (!v[y])
{
q[++tail] = y;
v[y] = 1;
}
}
}
}
if (p)
return false;
return true;
}
int main()
{
int i, m, x, y, z;
double l = 0, r = 0, mid;
n = re();
m = re();
for (i = 1; i <= n; i++)
fun[i] = re();
for (i = 1; i <= m; i++)
{
x = re();
y = re();
z = re();
r += z;
add(x, y, z);
}
r /= 2;
while (l + 1e-3 < r)
{
mid = (l + r) / 2;
if (judge(mid))
l = mid;
else
r = mid;
}
printf("%.2f", l);
return 0;
}

POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows的更多相关文章

  1. 洛谷 2868 [USACO07DEC]观光奶牛Sightseeing Cows

    题目戳这里 一句话题意 L个点,P条有向边,求图中最大比率环(权值(Fun)与长度(Tim)的比率最大的环). Solution 巨说这是0/1分数规划. 话说 0/1分数规划 是真的难,但貌似有一些 ...

  2. 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows

    P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题目描述 Farmer John has decided to reward his cows for their har ...

  3. 洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

  4. 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

  5. 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)

    题意 题目链接 Sol 复习一下01分数规划 设\(a_i\)为点权,\(b_i\)为边权,我们要最大化\(\sum \frac{a_i}{b_i}\).可以二分一个答案\(k\),我们需要检查\(\ ...

  6. 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解

    题面 这道题是一道标准的01分数规划: 但是有一些细节可以优化: 不难想到要二分一个mid然后判定图上是否存在一个环S,该环是否满足∑i=1t(Fun[vi]−mid∗Tim[ei])>0 但是 ...

  7. Luogu 2868 [USACO07DEC]观光奶牛Sightseeing Cows

    01分数规划复习. 这东西有一个名字叫做最优比率环. 首先这个答案具有单调性,我们考虑如何检验. 设$\frac{\sum_{i = 1}^{n}F_i}{\sum_{i = 1}^{n}T_i} = ...

  8. P2868 [USACO07DEC]观光奶牛Sightseeing Cows

    P2868 [USACO07DEC]观光奶牛Sightseeing Cows [](https://www.cnblogs.com/images/cnblogs_com/Tony-Double-Sky ...

  9. [USACO07DEC]观光奶牛Sightseeing Cows 二分答案+判断负环

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

随机推荐

  1. Pandas数据排序

    Pandas数据排序 .sort_index() 在指定轴上根据索引进行排序,索引排序后内容会跟随排序 b = pd.DataFrame(np.arange(20).reshape(4,5),inde ...

  2. php输出textarea数据(入库没有处理的)

    str_replace("\r\n","<br />",$xmactivity['xmdetail']) 导出excel换行方法 str_repla ...

  3. mongo的csv文件参考

    https://blog.csdn.net/u012318074/article/details/77713228

  4. CentOS 6.5下Redis安装测试

    NoSQL之Redis - CentOS 6.5安装测试 1.下载redis 可以在线安装或者下载 redis ①在线安装前需要检测是否存在rpm包不存在的话查看yum在线是否存在rpm包不存在的话就 ...

  5. 解题1(Suduku)

    题目描述 问题描述:数独(Sudoku)是一款大众喜爱的数字逻辑游戏.玩家需要根据9X9盘面上的已知数字,推算出所有剩余空格的数字,并且满足每一行.每一列.每一个粗线宫内的数字均含1-9,并且不重复. ...

  6. Java基础学习笔记(三)

    18.2.1无参无返回值的方法 18.2.1.1定义方法的语法格式 public static void 方法名称(){ 方法体 } 方法调用 类名.方法名称(); 注意:在Java中,同一个类中的方 ...

  7. django admin后台显示中文

    在settings中设置 LANGUAGE_CODE = ‘zh-Hans’

  8. STL::map/multimap

    map: 默认根据 key 排序(从小到大),能够通过 backet operator(operator [ ]) 来获取元素,内部由二叉搜索树来实现(binary search trees). mu ...

  9. Fraction to Recurring Decimal(STRING-TYPE CONVERTION)

    QUESTION Given two integers representing the numerator and denominator of a fraction, return the fra ...

  10. Codeforces Round #518 (Div. 2) [Thanks, Mail.Ru!]

    Codeforces Round #518 (Div. 2) [Thanks, Mail.Ru!] https://codeforces.com/contest/1068 A #include< ...