【题目链接】 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. MySQL使用笔记(七)排序和限制数据记录查询

    By francis_hao    Dec 17,2016 排序数据记录查询 排序是指将筛选出符合条件的数据进行有序排放,有升序(ASC(默认))方式和降序(DESC)方式. mysql> se ...

  2. Codeforces Round #520 (Div. 2) B. Math

    B. Math time limit per test:1 second memory limit per test:256 megabytes Description: JATC's math te ...

  3. 状压DP初识~~炮兵阵地

    炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31718   Accepted: 12253 Descriptio ...

  4. LA2995 Image is everything

    蓝书P12 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm ...

  5. LOJ tangjz的背包

    题目大意 有 \(n\) 个物品, 第 \(i\) 个物品的体积为 \(i\) 令 \(f(x)\) 为 选择 \(m\) 个物品, 体积和为 \(x\) 的方案数 令 \(V = \sum_{i=1 ...

  6. openstack 问题澄清

    1. neutron中plugin与agent是一一对应的吗? 在不使用ml2时,plugin与agent一一对应,如ovs-plugin与ovs-agent:当使用ml2 plugin时,该plug ...

  7. bzoj 1083 最小生成树

    裸的最小生成树. /************************************************************** Problem: User: BLADEVIL Lan ...

  8. DDCTF - evil 一个伪装成docx的exe

    0x01 题目描述 题目比较简单,不过这种题感觉比单纯的逆向算法来有意思的多,所以记录一下~ 0x02 脱壳 先拖到IDA瞅一眼,发现加壳了 用PEID查一下是什么壳,但是没有查出来.使用String ...

  9. 定制一个支持中英文的简单LaTex模板

    平常写汇报文档什么的,word排版有时还是比较费劲,遂定制一个简单的LaTex模板,中文默认为宋体,英文为LaTex默认字体,支持彩色高亮展示,有目录书签,有页眉展示,大致如下: LaTex代码如下: ...

  10. ZJOI2006书架

    追yql做题记录的时候做到的……一道Splay模版题…… 啊LCT写久了都有点忘了Splay了(什么奇怪的逻辑?) 其实说白了五个操作: 1. 将某元素置顶:将元素旋到根,然后将左子树合并到该元素的后 ...