题目链接   (双层图, 一层维护工作,一层维护政策)

 #include <bits/stdc++.h>
using namespace std; inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} /********************************************************************/ const int MAXN = ;
const int MAXM = ;
const int INF = 0x3f3f3f3f; struct Edge
{
int to, next, cap, flow, cost;
int x, y;
} edge[MAXM],HH[MAXN],MM[MAXN];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N, M;
char map[MAXN][MAXN];
void init()
{
N = MAXN;
tol = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v, int cap, int cost)//左端点,右端点,容量,花费
{
edge[tol]. to = v; edge[tol]. cap = cap; edge[tol]. cost = cost;
edge[tol]. flow = ; edge[tol]. next = head[u]; head[u] = tol++;
edge[tol]. to = u; edge[tol]. cap = ; edge[tol]. cost = -cost;
edge[tol]. flow = ; edge[tol]. next = head[v]; head[v] = tol++;
}
bool spfa(int s, int t)
{
queue<int>q;
for(int i = ; i < N; i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -;
}
dis[s] = ;
vis[s] = true;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -; i = edge[i]. next)
{
int v = edge[i]. to;
if(edge[i]. cap > edge[i]. flow &&
dis[v] > dis[u] + edge[i]. cost )
{
dis[v] = dis[u] + edge[i]. cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t] == -) return false;
else return true;
}
//返回的是最大流, cost存的是最小费用
int minCostMaxflow(int s, int t, int &cost)
{
int flow = ;
cost = ;
while(spfa(s,t))
{
int Min = INF;
for(int i = pre[t]; i != -; i = pre[edge[i^]. to])
{
if(Min > edge[i]. cap - edge[i]. flow)
Min = edge[i]. cap - edge[i]. flow;
}
for(int i = pre[t]; i != -; i = pre[edge[i^]. to])
{
edge[i]. flow += Min;
edge[i^]. flow -= Min;
cost += edge[i]. cost * Min;
}
flow += Min;
}
return flow;
}
const int inf = 1e9;
int P[],p,q,S[],T[];
void solve()
{
init();
int m,n,k,sum=;
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
scanf("%d",&P[i]),sum+=P[i];
scanf("%d%d%d",&m,&p,&q);
for(int i=;i<=m;i++)
scanf("%d%d",&S[i],&T[i]);
int st=,ed=;
for(int i=;i<=n;i++)
{
addedge(st,i,P[i],);
addedge(n+i,ed,P[i],);
}
addedge(st,n+,k,);
for(int i=p;i<=n;i++)
addedge(st,n+i,inf,q);
for(int i=;i<n;i++)
addedge(i,i+,inf,);
for(int i=;i<n;i++)
addedge(n+i,n+i+,inf,);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
if(i+T[j]<=n)
addedge(i,n+i+T[j],inf,S[j]);
}
int ans1=,ans2=;
ans1=minCostMaxflow(st,ed,ans2);
if(ans1==sum)printf("%d\n",ans2);
else printf("No solution\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--)solve();
return ;
}

King's Pilots的更多相关文章

  1. HDU 5644 King's Pilots 费用流

    King's Pilots 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5644 Description The military parade w ...

  2. HDU 5644 (费用流)

    Problem King's Pilots (HDU 5644) 题目大意 举办一次持续n天的飞行表演,第i天需要Pi个飞行员.共有m种休假计划,每个飞行员表演1次后,需要休假Si天,并提供Ti报酬来 ...

  3. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  4. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  5. [bzoj1087][scoi2005]互不侵犯king

    题目大意 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上 左下右上右下八个方向上附近的各一个格子,共8个格子. 思路 首先,搜索可以放弃,因为这是一 ...

  6. King's Quest —— POJ1904(ZOJ2470)Tarjan缩点

    King's Quest Time Limit: 15000MS Memory Limit: 65536K Case Time Limit: 2000MS Description Once upon ...

  7. 【状压DP】bzoj1087 互不侵犯king

    一.题目 Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上.下.左.右,以及左上.左下.右上.右下八个方向上附近的各一个格子,共8个格子. I ...

  8. ZOJ 2334 Monkey King

    并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子                       Monkey King ...

  9. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 K. King’s Rout

    K. King's Rout time limit per test 4 seconds memory limit per test 512 megabytes input standard inpu ...

随机推荐

  1. elementaryos必装软件

    所使用版本:elementaryos-0.4-stable-amd64.20160909.iso vmtools jdk sougouinput IntellijIEAD

  2. JS获取首字母

    function pySegSort(arr, empty) { if (!String.prototype.localeCompare) return null; var letters = &qu ...

  3. mybatis传递多个参数值(转)

    Mybatis传递多个参数   ibatis3如何传递多个参数有两个方法:一种是使用Map,另一种是使用JavaBean. <!--      使用HashMap传递多个参数      para ...

  4. Meta viewport 学习整理

    The meta viewport tag contains instructions to the browser in the matter of viewports and zooming. I ...

  5. P2743(poj1743) Musical Themes[差分+后缀数组]

    P2743 乐曲主题Musical Themes(poj1743) 然后呢这题思路其实还是蛮简单的,只是细节特别多比较恶心,忘记了差分带来的若干疏漏.因为转调的话要保证找到相同主题,只要保证一段内相对 ...

  6. bzoj 3230 相似子串 —— 后缀数组+二分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3230 先算出每个后缀贡献子串的区间: 然后前缀LCP直接查询,后缀LCP二分长度,查询即可: ...

  7. ng2父子模块数据交互

    一.父模块向子模块传值 //父html <my-child [childdata]="parentdata"></my-child> 其中,my-child ...

  8. java——类的封装

    public void setName(String str) { name=str; } public void setAge(int a) //set方法需要在括号中定义数据类型 { if(a&g ...

  9. lua调用c函数

    参考:http://blog.163.com/madahah@126/blog/static/170499225201121504936823/ 1.编辑C程序 vim luac.c #include ...

  10. HashMap为什么是线程不安全的

    HashMap底层是一个Entry数组,当发生hash冲突的时候,hashmap是采用链表的方式来解决的,在对应的数组位置存放链表的头结点.对链表而言,新加入的节点会从头结点加入. 我们来分析一下多线 ...