【题目链接】 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2230

【题目大意】

  给出一张图,从1到n的最长路不变的情况下,
  还能在不同的点之间增加最长总和的路为多长。

【题解】

  From http://www.hankcs.com/program/algorithm/aoj-2230-how-to-create-a-good-game.html  

  如果将原DAG权值取反,然后从最后一关连一条正权边到第一关,
  权值是最短路(负权值最短路=传统意义上的最长路)的长度的话,
  那么那些正圈中的负权边就是应该增加权值的边,具体应该加多少,就是正圈的权值。
  新建源点汇点,对于所有顶点,如果入度>出度,从源点连一条边到它,
  否则,从它连一条边到汇点,容量都是是度数差。

【代码】

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int INF=0x3f3f3f3f;
struct edge{
int to,cap,cost,rev;
edge(int to,int cap,int cost,int rev):to(to),cap(cap),cost(cost),rev(rev){}
};
const int MAX_V=10010;
int V,dist[MAX_V],prevv[MAX_V],preve[MAX_V];
vector<edge> G[MAX_V];
void add_edge(int from,int to,int cap,int cost){
G[from].push_back(edge(to,cap,cost,G[to].size()));
G[to].push_back(edge(from,0,-cost,G[from].size()-1));
}
int min_cost_flow(int s,int t,int f){
int res=0;
while(f>0){
fill(dist,dist+V,INF);
dist[s]=0;
bool update=1;
while(update){
update=0;
for(int v=0;v<V;v++){
if(dist[v]==INF)continue;
for(int i=0;i<G[v].size();i++){
edge &e=G[v][i];
if(e.cap>0&&dist[e.to]>dist[v]+e.cost){
dist[e.to]=dist[v]+e.cost;
prevv[e.to]=v;
preve[e.to]=i;
update=1;
}
}
}
}
if(dist[t]==INF)return -1;
int d=f;
for(int v=t;v!=s;v=prevv[v]){
d=min(d,G[prevv[v]][preve[v]].cap);
}f-=d;
res+=d*dist[t];
for(int v=t;v!=s;v=prevv[v]){
edge &e=G[prevv[v]][preve[v]];
e.cap-=d;
G[v][e.rev].cap+=d;
}
}return res;
}
void clear(){for(int i=0;i<V;i++)G[i].clear();}
const int MAX_N=120;
int N,M;
int in[MAX_N],out[MAX_N],tot;
void solve(){
int s=N,t=N+1;V=t+1;tot=0;
clear();
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int i=0;i<M;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add_edge(x,y,INF,-z);
out[x]++;in[y]++;
tot+=z;
}int max_f=0;
for(int i=0;i<N;i++){
if(in[i]>out[i]){
add_edge(s,i,in[i]-out[i],0);
max_f+=in[i]-out[i];
}else{
add_edge(i,t,out[i]-in[i],0);
}
}min_cost_flow(0,N-1,1);
add_edge(N-1,0,INF,-dist[N-1]);
printf("%d\n",min_cost_flow(s,t,max_f)-tot);
}
int main(){
while(~scanf("%d%d",&N,&M)){
solve();
}return 0;
}

AOJ 2230 How to Create a Good Game(费用流)的更多相关文章

  1. AOJ 2266 Cache Strategy(费用流)

    [题目链接] http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2266 [题目大意] 有M个桶,N个球,球编号为1到N,每个球都有重量 ...

  2. .NET基础拾遗(4)委托、事件、反射与特性

    Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理基础 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开 ...

  3. ASP.NET MVC5+EF6+EasyUI 后台管理系统(62)-EF链接串加密

    系列目录 前言: 这一节提供一个简单的功能,这个功能看似简单,找了一下没找到EF链接数据库串的加密帮助文档,只能自己写了,这样也更加符合自己的加密要求 有时候我们发布程序为了避免程序外的SQL链接串明 ...

  4. .net AES加密解密

    using System;      using System.Collections.Generic;      using System.Text;      using System.Secur ...

  5. MVC中使用WebMail 发送注册验证信息

    在MVC中发送Email 可以使用WebMail :使用起来十分简单.如下: WebMail.SmtpServer = ConfigurationHelper.GetValue("SmtpS ...

  6. C#实现AES加解密方法

    using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptograph ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. hadoop_并行写操作思路

    这篇文章是关于,如何修改hadoop的src以实现在client端上传大文件到HDFS的时候, 为了提高上传的效率实现将文件划分成多个块,将块并行的写入到datanode的各个block中 的初步的想 ...

  9. Windows Phone 学习笔记(一) 数据存储

    独立存储设置IsolatedStorageSetting private IsolatedStorageSettings _appSettings; public MainPage() { Initi ...

随机推荐

  1. 如何用JavaScript做一个可拖动的div层

    可拖动的层在Web设计中用处很多,比如在某些需要自定义风格布局的应用中,控件就需要拖动操作,下面介绍一个,希望可以满足你的需求,顺便学习一下可拖动的层是如何实现的. 下面是效果演示: 这个DIV可以移 ...

  2. hadoop SecondNamenode 详解

    SecondNamenode名字看起来很象是对第二个Namenode,要么与Namenode一样同时对外提供服务,要么相当于Namenode的HA. 真正的了解了SecondNamenode以后,才发 ...

  3. ubuntu使用su切换root用户提示“认证失败”

    在虚拟机上安装了ubuntu,安装时提示设置密码,也设置了,但是在终端操作时,遇到权限不够的问题,于是就想到就是要切换root用户,获取最高权限. 当我使用 su 切换到root用户时,提示我输入密码 ...

  4. Java多线程-一个简单的线程,实现挂起和恢复的功能

    public class MySprite implements Runnable { /* * 线程用变量 */ private boolean running = false; private b ...

  5. 100个Swift必备Tips(第二版)

    100个Swift必备Tips(第二版) 新年第一天,给大家一本电子书,希望新的一年里,步步高升. GitHub

  6. MyBatis系列四 之 智能标签进行查询语句的拼接

    MyBatis系列四 之 智能标签进行查询语句的拼接 使用Foreach进行多条件查询 1.1 foreach使用数组进行多条件查询 在MyBatis的映射文件中进行如下配置 <!--根据数组进 ...

  7. 01-modal Demo示例程序源代码

    源代码下载链接:01-modal.zip37.8 KB // MJAppDelegate.h // //  MJAppDelegate.h //  01-modal // //  Created by ...

  8. Windows autoKeras的下载与安装连接

    autoKeras autoKeras GitHub:https://github.com/jhfjhfj1/autokeras 百度网盘下载地址:http://pandownload.com/ 大牛 ...

  9. go的websocket实现

    websocket分为握手和数据传输阶段,即进行了HTTP握手 + 双工的TCP连接 RFC协议文档在:http://tools.ietf.org/html/rfc6455 握手阶段 握手阶段就是普通 ...

  10. HDU1045(二分图经典建模)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...