Sending Secret Messages LightOJ - 1404

Alice wants to send Bob some confidential messages. But their internet connection is not secured enough. As their names have been used in many networking schemes, they are very rich now. So, they don't want to send encoded messages, they want to use secured dedicated connection for them. So, they talked to some ISPS (Internet Service Providers) about their problem. Only they get is that there are N routers in the network, some of them share bidirectional links. Each link has a capacity, and for each KB of data passing through this link, they have to pay some money. Assume that Alice is connected with the 1st router and Bob is connected to the Nth router.

For example, in the picture, Alice wants to send 4 KB data from router 1 to router 6. Each link is identified by two integers in the form (a, b) where 'a' denotes the capacity of the link and 'b'denotes per KB cost of the link. So, Alice can send 1KB of data through 1 - 2 - 3 - 4 - 6 (cost 8), 2KB data through 1 - 5 - 6 (cost 2 * 9=18) and 1KB data through 1 - 3 - 4 - 6 (cost 11). So, the total cost is 37 units.

Now Alice wants to send P KB of data to Bob. You have to find the minimum amount of money they have to pay to achieve their goal.

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

Each case starts with a blank line. Next line contains three integers N (2 ≤ N ≤ 50)M (0 ≤ M ≤ N*(N-1)/2) and P (1 ≤ P ≤ 1000), where M denotes the number of bidirectional links. Each of the next M lines contains four integers u v w c (1 ≤ u, v ≤ N, u ≠ v, 1 ≤ w, c ≤ 100), meaning that there is a link between router u and v, and at most c KB data can be sent through this link, and each KB of data through this link will cost w. You can assume that there will be at most one connection between a pair of routers.

Output

For each case, print the case number and the minimum amount of money required or "impossible" if it's not possible to send P KB of data.

Sample Input

3

6 9 4

3 1 9 8

1 2 1 2

1 5 6 1

5 6 2 8

6 4 2 2

4 2 7 6

2 6 7 9

3 4 5 1

3 2 2 3

6 9 9

3 1 9 8

1 2 1 2

1 5 6 1

5 6 2 8

6 4 2 2

4 2 7 6

2 6 7 9

3 4 5 1

3 2 2 3

4 4 20

1 3 1 3

3 4 1 4

1 2 1 2

2 4 1 5

Sample Output

Case 1: 37

Case 2: 139

Case 3: impossible

题意:给定一个图,有n个顶点m条无向边,每条边都有容量和费用,求从1到n穿输p的数据量时的费用。

题解:费用流模板,EK算法介绍  https://blog.csdn.net/y990041769/article/details/21026445

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int MAXX=;
const int INF=0x3f3f3f3f; struct node
{
int st;
int to;
int next;
int cap;
int cost;
}edge[MAXX]; int head[MAXX],tol;
int pre[MAXX],dis[MAXX];
bool vis[MAXX];
int n,m,p; void init()
{
tol=;
memset(head,-,sizeof(head));
} void addedge(int u,int v,int cap,int cost)
{
edge[tol].st=u;
edge[tol].to=v;
edge[tol].cap=cap;
edge[tol].cost=cost;
edge[tol].next=head[u];
head[u]=tol++; edge[tol].st=v;
edge[tol].to=u;
edge[tol].cap=;
edge[tol].cost=-cost;
edge[tol].next=head[v];
head[v]=tol++;
} int minCostMaxFlow(int s,int t,int p)
{
int cost=;
while(p>)
{
queue<int> q;
memset(dis,INF,sizeof(dis));
memset(vis,,sizeof(vis));
memset(pre,-,sizeof(pre));
dis[s]=;
vis[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front(); q.pop();
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int to=edge[i].to;
if(edge[i].cap>&&dis[to]>dis[u]+edge[i].cost)
{
dis[to]=dis[u]+edge[i].cost;
pre[to]=i;
if(!vis[to])
{
vis[to]=;
q.push(to);
}
}
}
}
if(dis[t]==INF)return -;
int minn=p; for(int i=pre[t];i!=-;i=pre[edge[i].st])
minn=min(minn,edge[i].cap); for(int i=pre[t];i!=-;i=pre[edge[i].st])
{
edge[i].cap-=minn;
edge[i^].cap+=minn;
}
cost+=minn*dis[t];
p-=minn;
}
return cost;
} int main()
{
int T,x,y,z,w,cas=;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&p);
init();
for(int i=;i<=m;i++)
{
scanf("%d%d%d%d",&x,&y,&z,&w);
addedge(x,y,z,w);
addedge(y,x,z,w);
}
int ans=minCostMaxFlow(,n,p);
printf("Case %d: ",cas++);
if(ans!=-) printf("%d\n",ans);
else printf("impossible\n");
}
return ;
}

Sending Secret Messages LightOJ - 1404的更多相关文章

  1. How the Bitcoin protocol actually works

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  2. [USACO 08DEC]Secret Message

    Description Bessie is leading the cows in an attempt to escape! To do this, the cows are sending sec ...

  3. Secret Message ---- (Trie树应用)

    Secret Message   总时间限制:  2000ms  内存限制:  32768kB 描述 Bessie is leading the cows in an attempt to escap ...

  4. [USACO08DEC] 秘密消息Secret Message

    题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...

  5. 「USACO08DEC」「LuoguP2922」秘密消息Secret Message(AC自动机

    题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...

  6. 洛谷 P2922 [USACO08DEC]秘密消息Secret Message

    题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...

  7. Distributed Cache Coherence at Scalable Requestor Filter Pipes that Accumulate Invalidation Acknowledgements from other Requestor Filter Pipes Using Ordering Messages from Central Snoop Tag

    A multi-processor, multi-cache system has filter pipes that store entries for request messages sent ...

  8. Fedora 24中的日志管理

    Introduction Log files are files that contain messages about the system, including the kernel, servi ...

  9. window下安装jupyter

    1.Install [Anaconda](https://docs.continuum.io/anaconda/install#anaconda-install) 实际上安装了anaconda就已经安 ...

随机推荐

  1. 小胖说事22-----iOS开发技巧之取消键盘响应和截屏功能

    1.UILable内容模糊 在非Retina的iPad mini 的屏幕上,一个UILable的frame的origin值假设是有小数位(如0.5),就会造成显示模糊,所以不妨用整数值的origin. ...

  2. 关于PROFIBUS Master(H)不能正确识别并处理 DP-Slave 回复的RS帧的一些思考

    图1.是在測试过程中,发现PROFIBUS Master(H)不能正确识别并处理 DP-Slave 回复的RS帧.引起Slave回复 RS 帧的操作是"断开Slave与Master之间的PR ...

  3. 调用线程必须为 STA,因为许多 UI 组件都需要

    WPF中,代码中准备控制控件内容时,有时会报错: 调用线程必须为 STA,因为许多 UI 组件都需要 我知道,在winform下面,使用多线程时,控件的值读取是可以的,但如果要更改,那么就必须进行一些 ...

  4. 【HNOI 2003】 激光炸弹

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1218 [算法] 二维前缀和 [代码] #include<bits/stdc++ ...

  5. Makefile 实际用例分析(二) ------- 比较通用的一种架构

    之前已经讲了这一篇文章:Makefile实际用例分析(一)-----比较通用的一种架构 现在这篇其实和那个差的不是很多,只是在布局上有些差别(这个makefile也是论坛上一起讨论过的,囧,忘了哪个论 ...

  6. MySQL 1366错误解决办法

    MySQL 1366错误大致描述如下 SQL Error: 1366: Incorrect string value: "xE8xAFxA6xE7xBBx86-" for colu ...

  7. Tempter of the Bone------剪枝

    看了好多别人的  代码,最终还是 感觉 这种代码的风格适合我  下面附上代码 /* 首先 应该充满信心! 先写出来 自己的程序 然后慢慢改 , 如果是 答题思路错误的话 借鉴别人的 代码 再写 */ ...

  8. docker容器如何安装vim

    mv /etc/apt/sources.list /etc/apt/sources.list.bak && \     echo "deb http://mirrors.16 ...

  9. PHP 在线 编辑 解析

    http://www.w3schools.com/php/default.asp    http://www.w3schools.com/php/showphp.asp?filename=demo_s ...

  10. layer实现在前台删除前确认弹出框,并回调后台删除事件

    最近遇到一些问题,用layer提示消息框,比如删除时,提示消息确定后 return false时,则不管用了,因为layer不支持阻塞,下面就解决一下这个问题:(本文使用Xcode,如要学习,请复下面 ...