首页 技术 正文
技术 2022年11月6日
0 收藏 848 点赞 813 浏览 1819 个字

2016青岛现场赛的一题,由于第一次走过不会产生影响,需要拆点,不过比赛时没想到,此外还有许多细节要注意,如要加eps,时间卡得较紧要注意细节优化等

#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int N = 108, M = 50000;
const int INF = 1e9;
const double eps = 1e-7;
int q[1000000];
struct Node{
int u, v, cap;
double cost;
int next;
}edge[M];//有向图,u到v的容量,费用
int tot;
int head[N], pre[N], path[N];
double dis[N];
bool inq[N];void init(){
tot = 0;
memset(head, -1, sizeof(head));
}void add(int u, int v, int cap, double cost){
edge[tot].u = u;
edge[tot].v = v;
edge[tot].cap = cap;
edge[tot].cost = cost;
edge[tot].next = head[u];
head[u] = tot++;
edge[tot].u = v;
edge[tot].v = u;
edge[tot].cap = 0;
edge[tot].cost = -cost;
edge[tot].next = head[v];
head[v] = tot++;
}bool SPFA(int st, int des){
memset(inq, 0, sizeof(inq));
fill(dis, dis + des + 2, INF);
memset(pre, -1, sizeof(pre));
int front = 0, rear = 0;
q[rear++] = st;
dis[st] = 0;
inq[st] = true;
while(front < rear){
int u = q[front++];inq[u] = false;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].v;
if(edge[i].cap > 0 && dis[v] > dis[u] + edge[i].cost + eps){
dis[v] = dis[u] + edge[i].cost;
pre[v] = u;
path[v] = i;
if(!inq[v]){
inq[v] = true;
q[rear++] = v;
}
}
}
}
return pre[des] != -1;
//return dis[des] + eps < INF;
}double EdmondsKarp(int st, int des){
double mincost = 0, flow = 0;
while(SPFA(st, des)){
int f = INF;
for(int i = des; i != st; i = pre[i]){
if(f > edge[path[i]].cap){
f = edge[path[i]].cap;
}
}
for(int i = des; i != st; i = pre[i]){
edge[path[i]].cap -= f;
edge[path[i]^1].cap += f;
}
mincost += f * dis[des];
flow += f;
}
return mincost;
}
int main(){
int t;
cin >>t;
while(t--){
int n, m;
scanf("%d %d", &n, &m);
init();
int st = 0, des = n + 1;
for(int i = 1; i <= n; i++){
int si, bi;
scanf("%d %d", &si, &bi);
if(si > bi){
add(st, i, si - bi, 0.0);
}else if(si < bi){
add(i, des, bi - si, 0.0);
}
}
while(m--){
int u, v, c;
double p;
scanf("%d %d %d %lf", &u, &v, &c, &p);
if(c > 0){
add(u, v, 1, 0);
if(c > 1){
add(u, v, c - 1, -log10(1 - p));
}
}
}
printf("%.2f\n", 1.0 - pow(10.0, -EdmondsKarp(st, des)));}
return 0;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731