POJ2135 来回最短路(简单费用流)
题意:
就是从1走到n然后再走回来,一条边只能走一次,要求路径最短。
思路:
比较水,可以直接一遍费用流,不解释了,具体的看看代码,敲这个题就是为了练
练手,好久不敲了,怕比赛手生。
#include<queue>
#include<stdio.h>
#include<string.h>
#define N_node 1000 + 10
#define N_edge 40000 + 20
#define INF 100000000
using namespace std;
typedef struct
{
int from ,to ,cost ,flow ,next;
}STAR;
STAR E[N_edge];
int list[N_node] ,tot;
int mer[N_edge];
int s_x[N_node];
void add(int a ,int b ,int c ,int d)
{
E[++tot].from = a;
E[tot].to = b;
E[tot].cost = c;
E[tot].flow = d;
E[tot].next = list[a];
list[a] = tot;
E[++tot].from = b;
E[tot].to = a;
E[tot].cost = -c;
E[tot].flow = 0;
E[tot].next = list[b];
list[b] = tot;
}
bool Spfa(int s ,int t ,int n)
{
int mark[N_node] = {0};
for(int i = 0 ;i <= n ;i ++) s_x[i] = INF;
mark[s] = 1 ,s_x[s] = 0;
queue<int>q;
q.push(s);
memset(mer ,255 ,sizeof(mer));
while(!q.empty())
{
int xin ,tou = q.front();
q.pop();
mark[tou] = 0;
for(int k = list[tou] ;k ;k = E[k].next)
{
xin = E[k].to;
if(s_x[xin] > s_x[tou] + E[k].cost && E[k].flow)
{
s_x[xin] = s_x[tou] + E[k].cost;
mer[xin] = k;
if(!mark[xin])
{
mark[xin] = 1;
q.push(xin);
}
}
}
}
return mer[t] != -1;
}
int M_C_Flow(int s ,int t ,int n)
{
int maxflow = 0 ,mincost = 0 ,minflow;
while(Spfa(s ,t ,n))
{
minflow = INF;
for(int i = mer[t] ;i + 1 ;i = mer[E[i].from])
if(minflow > E[i].flow) minflow = E[i].flow;
for(int i = mer[t] ;i + 1 ;i = mer[E[i].from])
{
E[i].flow -= minflow;
E[i^1].flow += minflow;
mincost += minflow * E[i].cost;
}
maxflow += minflow;
}
return mincost;
}
int main ()
{
int n ,m ,i ,a ,b ,c;
while(~scanf("%d %d" ,&n ,&m))
{
memset(list ,0 ,sizeof(list)) ,tot = 1;
for(i = 1 ;i <= m ;i ++)
{
scanf("%d %d %d" ,&a ,&b ,&c);
add(a ,b ,c ,1);
add(b ,a ,c ,1);
}
add(0 ,1 ,0 ,2);
add(n ,n + 1 ,0 ,2);
printf("%d\n" ,M_C_Flow(0 ,n + 1 ,n + 1));
}
return 0;
}
POJ2135 来回最短路(简单费用流)的更多相关文章
- POJ 2135 简单费用流
题意: 题意是一个人他要从牧场1走到牧场n然后在走回来,每条路径只走一次,问全程的最短路径是多少. 思路: 这个题目挺简单的吧,首先要保证每条边只能走一次,然后还要要求费用最 ...
- POJ3422简单费用流
题意: 给一个n*n的矩阵,从左上角走到右下角,的最大收益,可以走k次,每个格子的价值只能取一次,但是可以走多次. 思路: 比较简单的一个费用流题目,直接拆点,拆开的点之间连接两 ...
- POJ 2135 Farm Tour && HDU 2686 Matrix && HDU 3376 Matrix Again 费用流求来回最短路
累了就要写题解,近期总是被虐到没脾气. 来回最短路问题貌似也能够用DP来搞.只是拿费用流还是非常方便的. 能够转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1.然后连 ...
- 最短路&生成树&二分图匹配&费用流问题
最短路 题意理解,建图 https://vjudge.net/problem/UVALive-4128 飞机票+行程建图 https://vjudge.net/problem/UVALive-3561 ...
- 【bzoj2324】[ZJOI2011]营救皮卡丘 最短路-Floyd+有上下界费用流
原文地址:http://www.cnblogs.com/GXZlegend/p/6832504.html 题目描述 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘 ...
- Mining Station on the Sea HDU - 2448(费用流 || 最短路 && hc)
Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- poj2135 Farm Tour(费用流)
Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...
- HDU4807 Lunch Time(费用流变种)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4807 Description The campus of Nanjing Universit ...
- 洛谷P4003 无限之环(infinityloop)(网络流,费用流)
洛谷题目传送门 题目 题目描述 曾经有一款流行的游戏,叫做 Infinity Loop,先来简单的介绍一下这个游戏: 游戏在一个 n ∗ m 的网格状棋盘上进行,其中有些小方格中会有水管,水管可能在格 ...
随机推荐
- FreeBSD NGINX TCP转发
前几天搞转发,研究了下TCP转发,现在记录下来 首先加载模块 注意:这是FreeBSD的位置.并且需要NGINX支持 load_module /usr/local/libexec/nginx/ngx_ ...
- WPF 基础 - Binding 的源与路径
1. 源与路径 把控件作为 binding 源与 binding 标记拓展: 控制 Binding 的方向及数据更新: Binding 的路径 Path: 没有路径的 Binding: 为 Bindi ...
- Apache配置 9.访问控制-Diretory\FileMatch
(1)介绍 访问控制限制白名单IP,针对文件和目录. (2)目录配置 #vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <Virtua ...
- LeetCode 175. Combine Two Tables 【MySQL中连接查询on和where的区别】
一.题目 175. Combine Two Tables 二.分析 连接查询的时候要考虑where和on的区别 where : 查询时,连接的时候是必须严格满足条件的,满足了才会加入到临时表中. on ...
- P1089_津津的储蓄计划(JAVA语言)
package 顺序与分支; /* * 题目描述 津津的零花钱一直都是自己管理.每个月的月初妈妈给津津300元钱, 津津会预算这个月的花销,并且总能做到实际花销和预算的相同. 为了让津津学习如何储蓄, ...
- 真会C#微信小程序的习题数据JSON文件下载链接
完全没有精力去维护了,所以小程序停掉,集中精力做一件事. 链接: https://pan.baidu.com/s/1xL45KxDzR5oEQM6nwBA5rw 提取码: qv6n
- GoldenEye靶机work_through暨CVE-2013-3630复现
前言 备考OSCP,所以接下来会做一系列的OSCP向靶机来练手 靶机描述 I recently got done creating an OSCP type vulnerable machine th ...
- Git基础知识之内部状态管理系统
本文主要来介绍一下 Git 的内部状态管理系统.它利用基于节点和指针的数据结构来跟踪及管理编辑操作的时间线. 对本地项目而言,任一时刻,Git 处于三种状态中的一种:工作区状态.暂存区状态和提交区状态 ...
- JProfiler使用说明及常用案例分析
1 配置远程连接 (1)启动JProfiler,选择Attach to a running JVM (2)选择Quick Attach,然后选择On another computer,然后选择Edit ...
- 一文彻底掌握Apache Hudi的主键和分区配置
1. 介绍 Hudi中的每个记录都由HoodieKey唯一标识,HoodieKey由记录键和记录所属的分区路径组成.基于此设计Hudi可以将更新和删除快速应用于指定记录.Hudi使用分区路径字段对数据 ...