# 洛谷2494 [SDOI2011]保密 （分数规划+最小割）

$$a[i]=\frac{\sum t}{\sum s}$$

$$mid>\frac{\sum t}{\sum s}$$

$\sum t-\sum s\times mid<0$

qwq

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define mk make_pair
#define ll long long
#define db double
using namespace std;
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int maxn = 1410;
const int maxm = 1e6+1e2;
const double inf = 1e12;
const double eps = 1e-4 ;
int point[maxn],nxt[maxm],to[maxm];
db val[maxm];
int cnt=1,n,m;
int h[maxn];
int x[maxm],y[maxm];
double w[maxm],p[maxm];
int s,t;
int st;
double dis[maxn],cost[maxm];
int vis[maxn];
void init()
{
cnt=1;
memset(point,0,sizeof(point));
}
{
nxt[++cnt]=point[x];
to[cnt]=y;
point[x]=cnt;
val[cnt]=w;
}
{
//printf("%d %d %.2lf\n",x,y,w);
nxt[++cnt]=point[x];
to[cnt]=y;
cost[cnt]=w;
point[x]=cnt;
}
void insert(int x,int y,db w)
{
//cout<<x<<" "<<y<<" "<<w<<endl;
}
queue<int> q;
void spfa(int s,int ed)
{
for (int i=1;i<=maxn-2;i++) dis[i]=inf;
memset(vis,0,sizeof(vis));
while (!q.empty()) q.pop();
dis[s]=0;
q.push(s);
while (!q.empty())
{
int x = q.front();
q.pop();
vis[x]=0;
for (int i=point[x];i;i=nxt[i])
{
int p = to[i];
if (dis[p]>dis[x]+cost[i])
{
dis[p]=dis[x]+cost[i];
if (p==ed && dis[p]<=-eps) return;
if (!vis[p])
{
q.push(p);
vis[p]=1;
}
}
}
}
}
bool bfs(int s)
{
memset(h,-1,sizeof(h));
while (!q.empty()) q.pop();
h[s]=0;
q.push(s);
while (!q.empty())
{
int x = q.front();
q.pop();
for (int i=point[x];i;i=nxt[i])
{
int p = to[i];
if (h[p]==-1 && val[i]>0)
{
h[p]=h[x]+1;
q.push(p);
}
}
}
if (h[t]==-1) return false;
return true;
}
db dfs(int x,db low)
{
if (x==t ||low==0) return low;
db totflow=0;
for (int i=point[x];i;i=nxt[i])
{
int p = to[i];
if (val[i]>0 && h[p]==h[x]+1)
{
db tmp = dfs(p,min(low,val[i]));
val[i]-=tmp;
val[i^1]+=tmp;
low-=tmp;
totflow+=tmp;
}
}
if (low>0) h[x]=-1;
}
db dinic()
{
db ans=0;
while (bfs(s))
{
ans=ans+dfs(s,inf);
}
return ans;
}

double uu=0;
bool check(double mid,int xx)
{
init();
for (int i=1;i<=m;i++)
spfa(n,xx);
return (dis[xx]<=-eps);
}
double solve(int x)
{
double l=0,r=2e9;
double ans=inf;
while (r-l>=1e-4)
{
double mid = (l+r)/2;
if(check(mid,x)) r=mid,ans=mid;
else l=mid;
}
return ans;
}
double a[maxn];
signed main()
{
for (int i=1;i<=m;i++)
scanf("%lld%lld%lf%lf",&x[i],&y[i],&w[i],&p[i]);
init();
s=maxn-10;
t=s+1;
for (int i=1;i<=num2;i++)
{
double pp = solve(i);
a[i]=pp;
}
init();
for (int i=1;i<=num2;i++)
{
if (i&1) insert(s,i,a[i]);
else insert(i,t,a[i]);
}
for (int i=1;i<=num1;i++)
{
if (v&1) swap(u,v);
if (a[u]==inf && a[v]==inf)
{
cout<<-1;
return 0;
}
insert(u,v,inf);
}
double ptx = dinic();
printf("%.1lf\n",ptx);
return 0;
}


posted @ 2018-12-27 18:52  y_immortal  阅读(98)  评论(0编辑  收藏  举报