HDU 3488Tour(流的最小费用网络流)
职务地址:hdu3488
这题跟上题基本差点儿相同啊。。。
。
详情请戳这里。
另外我认为有要改变下代码风格了。。最终知道了为什么大牛们的代码的变量名都命名的那么长。。我决定还是把源点与汇点改成source和sink吧。。用s和t太easy冲突了。。
。于是如此简单的一道题调试到了如今。
。sad。。
。
代码例如以下:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <algorithm> using namespace std;
const int INF=0x3f3f3f3f;
int head[500], source, sink, cnt, flow, cost;
int d[500], pre[500], q[1000000], cur[500], vis[500];
struct node
{
int u, v, cap, cost, next;
}edge[1000000];
void add(int u, int v, int cap, int cost)
{
edge[cnt].v=v;
edge[cnt].cap=cap;
edge[cnt].cost=cost;
edge[cnt].next=head[u];
head[u]=cnt++; edge[cnt].v=u;
edge[cnt].cap=0;
edge[cnt].cost=-cost;
edge[cnt].next=head[v];
head[v]=cnt++;
}
int spfa()
{
memset(d,INF,sizeof(d));
memset(vis,0,sizeof(vis));
int minflow=INF, f1=0, f2=0, i;
q[f1++]=source;
d[source]=0;
cur[source]=-1;
while(f1>=f2)
{
int u=q[f2++];
vis[u]=0;
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(d[v]>d[u]+edge[i].cost&&edge[i].cap)
{
d[v]=d[u]+edge[i].cost;
if(minflow>edge[i].cap)
{
minflow=edge[i].cap;
}
cur[v]=i;
if(!vis[v])
{
q[f1++]=v;
vis[v]=1;
}
}
}
}
if(d[sink]==INF) return 0;
flow+=minflow;
cost+=minflow*d[sink];
for(i=cur[sink];i!=-1;i=cur[edge[i^1].v])
{
edge[i].cap-=minflow;
edge[i^1].cap+=minflow;
}
return 1;
}
int main()
{
int T, n, m, i, j, a, b, c;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
cnt=0;
source=0;
sink=2*n+1;
flow=0;
cost=0;
for(i=1;i<=n;i++)
{
add(source,i,1,0);
add(i+n,sink,1,0);
}
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b+n,1,c);
}
while(spfa());
printf("%d\n",cost);
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
HDU 3488Tour(流的最小费用网络流)的更多相关文章
- hdu 1533 Going Home 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...
- HDU 5988.Coding Contest 最小费用最大流
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- hdu 3667(拆边+最小费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 思路:由于花费的计算方法是a*x*x,因此必须拆边,使得最小费用流模板可用,即变成a*x的形式. ...
- hdu 3488(KM算法||最小费用最大流)
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- hdu 3395(KM算法||最小费用最大流(第二种超级巧妙))
Special Fish Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 1533 Going Home 最小费用最大流 入门题
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDU–5988-Coding Contest(最小费用最大流变形)
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- POJ 2195 & HDU 1533 Going Home(最小费用最大流)
这就是一道最小费用最大流问题 最大流就体现到每一个'm'都能找到一个'H',但是要在这个基础上面加一个费用,按照题意费用就是(横坐标之差的绝对值加上纵坐标之差的绝对值) 然后最小费用最大流模板就是再用 ...
- UVA 10594-Date Flow(无向图的最小费用网络流+题目给的数据有误)
题意:给一个有N个点的无向图,要求从1向N传送一定的数据,每条边的容量是一定的,如果能做到,输出最小的费用,否则输出Impossible. 解析:由于是无向图,所以每个有连接的两个点要建4条边,分别是 ...
随机推荐
- Principle of Computing (Python)学习笔记(5) BFS Searching + Zombie Apocalypse
1 Generators Generator和list comprehension非常类似 Generators are a kind of iterator that are defined l ...
- C语言,realloc
void * realloc ( void * ptr, size_t new_size ); 关于realloc的行为方式,结合源码总结为:1. realloc失败的时候,返回NULL: 2. re ...
- WEB应用如何解决安全退出问题
让我先来描述一下这个情况.一位用户第一次请求一个web页面,web应用弹出登录窗口提示用户登录,用户输入用户名,密码,验证码后服务器进行判断,正确后,返回用户请求的页面. 此时,用户有事需要离 ...
- office文档转pdf
这里贴下代码吧,没啥好说的. using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- 九度OJ 1065 输出梯形 (模拟)
题目1065:输出梯形 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3745 解决:2043 题目描写叙述: 输入一个高度h.输出一个高为h.上底边为h的梯形. 输入: 一个整数h(1& ...
- 怎样手势的判断android GestureDetector在android开发中
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view. ...
- js监听滚动条 回到顶端
效果:当出现滚动条,且滚动条出现移动时,把回到顶端按钮 显示出来:当滚动条回到顶部时,将回到顶端按钮隐藏. <script type="text/javascript"> ...
- PreTranslateMessage和TranslateMessage区别
PreTranslateMessage是消息在送给TranslateMessage函数之前被调用的,绝大多数本窗口的消息都要通过这里,比较常用,当需要在MFC之前处理某些消息时,常常要在这里添加代码. ...
- 【Demo 0003】Android 事件
本章学习要点: 1. 了解Android中资源用途: 2. 掌握资源使用通用规则: 3. 掌握具体资源使用方法;
- 【状态DP】 HDU 1074 Doing Homework
原题直通车:HDU 1074 Doing Homework 题意:有n门功课需要完成,每一门功课都有时间期限t.完成需要的时间d,如果完成的时间走出时间限制,就会被减 (d-t)个学分.问:按怎样 ...