init 一开始搞成2*n+2了...囧  所以初始化很重要!

然后提交的时候忘了删调试的数据了。。囧

技巧:设立虚拟节点

建图比较麻烦(非常)。

要考虑到保养完了的车可以免费再用

设立S,T  ,1-N,  N+1-2N

S连1-N,cap为ri,cost为0,表示用了的还没维护的车,还要链i,i+1

T连N+1 --2N,cap为ri   cost为0  表示目标

设立虚拟节点2*n+1,S连之,cap为ci,cost为pi,2*n+1再连到N+1 -- N,cap为ri,

表示可以花钱买

if(i+d[j]+1<=n) addedge(i,i+d[j]+1+n,INF,s[j]);      表示可以免费得到的维护过车

#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iostream>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;
int r[],c[],p[],d[],s[];
struct Edge {
int from, to, cap, flow, cost;
Edge(int u, int v, int c, int f, int w):from(u),to(v),cap(c),flow(f),cost(w) {}
}; struct MCMF {
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn]; // 是否在队列中
int d[maxn]; // Bellman-Ford
int p[maxn]; // 上一条弧
int a[maxn]; // 可改进量 void init(int n) {
this->n = n;
for(int i = ; i < n; i++) G[i].clear();
edges.clear();
} void Addedge(int from, int to, int cap, int cost) {
edges.push_back(Edge(from, to, cap, , cost));
edges.push_back(Edge(to, from, , , -cost));
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BellmanFord(int s, int t,int& flow, int& cost) {
for(int i = ; i < n; i++) d[i] = INF;
memset(inq, , sizeof(inq));
d[s] = ; inq[s] = ; p[s] = ; a[s] = INF; queue<int> Q;
Q.push(s);
while(!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = ;
for(int i = ; i < G[u].size(); i++) {
Edge& e = edges[G[u][i]];
if(e.cap > e.flow && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if(!inq[e.to]) { Q.push(e.to); inq[e.to] = ; }
}
}
}
if(d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
for(int u = t; u != s; u = edges[p[u]].from) {
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];
}
return true;
} // 需要保证初始网络中没有负权圈
int MincostFlow(int s, int t,int& cost) {
int flow = ; cost = ;
while(BellmanFord(s, t,flow, cost)); return flow;
} }; MCMF g; int main ()
{
int u,j,T,C,R,ans,n;
scanf("%d",&T);
for(int kase=; kase<=T; kase++)
{
ans=;
int cnt=;
scanf("%d%d%d",&n,&C,&R);
g.init(*n+); //init 一开始搞成2*n+2了...囧
for(int i=; i<=n; i++)
scanf("%d",&r[i]),ans+=r[i];
for(int i=; i<=C; i++)
scanf("%d%d",&c[i],&p[i]);
for(int i=; i<=R; i++)
scanf("%d%d",&d[i],&s[i]); for(int i=; i<=C; i++)
g.Addedge(,*n+,c[i],p[i]);
for(int i=; i<n; i++)
g.Addedge(i,i+,INF,); for(int i=; i<=n; i++)
g.Addedge(,i,r[i],);
for(int i=n+; i<=*n; i++)
g.Addedge(i,(n+)*,r[i-n],);
for(int i=n+; i<=*n; i++)
g.Addedge(*n+,i,INF,);
for(int i=; i<=n; i++)
{
for(int j=; j<=R; j++)
{
if(i+d[j]+<=n) g.Addedge(i,i+d[j]++n,INF,s[j]);
}
}
int cos;
int f=g.MincostFlow(,*n+,cos); printf("Case %d: ",kase);
if(f!=ans)
printf("impossible\n");
else
printf("%d\n",cos);
}
return ;
}
/*
2
3 2 1
10 20 30
40 90 15 100
1 5
*/

uva12433 Rent a Car的更多相关文章

  1. UVA12433 【Rent a Car】

    这题应该算是比较难的一道网络流的题,(但却在我校OJ考试上出现了),但是大家只要能理解此图的建边方式就行. 假设有5天的租车需求,虚拟出2*n+2 即 12个节点,0为源点,12为汇点. 1,源点到1 ...

  2. rent bike问题(二分+贪心)

    题目描述: A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to ...

  3. rent a apartment

    今日焦点 month-to-month 按月 6-month minimum 至少六个月 sublease 转租 drenched in sunlight 阳光充足的 词汇实践 I am lookin ...

  4. SPOJ RENT 01背包的活用+二分

    这个题目给定N航班的发出时间和结束时间以及价值,要求不冲突时间的最大价值 第一时间想到经典的N方DP,即对航班按发出时间排一下序之后每个i对前面的都扫一遍 时间过不了N有10万,只能想优化了,一开始想 ...

  5. SPOJ 130 - Rent your airplane and make money(dp+优化)

    题意:有n列预定航班,从st时刻开始出发,飞行时间为d,花费为p,且同一时刻不能有两个航班,求最大的花费 对航班的开始时间(或结束时间)按升序排序,从后往前找到对应结束时间所在的航班位置(如按结束时间 ...

  6. 用C++实现Linux中shell的ls功能

    实现输出当前目录下的文件名 ls功能: 方法一: #include <iostream> #include <algorithm> #include <stdio.h&g ...

  7. 设计模式-代理模式(Proxy Model)

    文 / vincentzh 原文连接:http://www.cnblogs.com/vincentzh/p/5988145.html 目录 1.写在前面 2.概述 3.目的 4.结构组成 5.实现 5 ...

  8. 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(前言)

    一直希望能够搭建一个完整的,基础Web框架,方便日后接一些外快的时候,能够省时省力,终于花了一周的时间,把这个东西搞定了.特此写下此博客,一来是纪念,二来是希望能够为别人提供方便.顺带说一下,恩,组合 ...

  9. 【Bootstrap-插件使用】Jcrop+fileinput组合实现头像上传功能

    作者:Dreawer链接:https://zhuanlan.zhihu.com/p/24465742来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:梦游的龙猫(转 ...

随机推荐

  1. saltstack自动化运维快速入门

    saltstack自动化运维快速入门 关于saltstack 这个软件是干啥的 我这里就不介绍了 只是简单的说下是干啥的 网上的说法是 它是func的强化版本+ puppet的精简版 关于puppet ...

  2. 【旧文章搬运】Windows句柄表分配算法分析(二)

    原文发表于百度空间,2009-03-30========================================================================== 四.句柄表 ...

  3. Centos系统真机安装,U盘方式

    下载Centos系统镜像,建议选择Minimal ISO.下载地址:https://www.centos.org/download/ 下载Fedora Media Writer,用来将系统镜像写到U盘 ...

  4. perceptron and ANN

    %% Perceptron Regression close all clear %%load data x = load('ex4x.dat'); y = load('ex4y.dat'); x=o ...

  5. k8s-基于canel的网络策略-十九

    一.前提 上一节学习了flannel,但是我们应该了解flannel只能提供网络通讯,而不能提供网络策略.因此,我们本节学习canal,让它来提供网络策略,来配合flannel使用. canal是ca ...

  6. UVa 11584 Partitioning by Palindromes (简单DP)

    题意:给定一个字符串,求出它最少可分成几个回文串. 析:dp[i] 表示前 i 个字符最少可分成几个回文串,dp[i] = min{ 1 + dp[j-1] | j-i是回文}. 代码如下: #pra ...

  7. Bid和Ask

    一直很懵到底哪个是哪个,记吧,很快就又懵了.网上又坑,每一个解释清楚的.这次搞明白了记下来. 当然,这么逗比的取名法我也是醉了.直接加点东西,UserBuy,UserSell,BankBuy,Bank ...

  8. 一个坑爹的Swift报错原因分析与解决方案

    有时候在实际开发中,完全没有任何问题.但是一到实机测试,就会直接卡机   let count = scoreStorage.count return scoreStorage[Int(arc4rand ...

  9. 793. Preimage Size of Factorial Zeroes Function

    Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...

  10. 664A - Complicated GCD

    题意真是七零八落,乱七八糟.盲目瞎写,水过就好? #include <cstdio> #include <cstring> #include <algorithm> ...