【题目链接】 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. webstorm vue cli 热更新不起作用解决办法

    在网上搜到的:原因是(webstorm默认保存在临时文件)  连接  1.打开设置 2.把 System Settings => Synchornization => 最后一项勾去掉

  2. jw player学习笔记----跨域请求

    需求来源:播放器皮肤文件请求不到,被限制了. 参考官网解决方案: http://www.longtailvideo.com/support/jw-player/28844/crossdomain-fi ...

  3. php getimagesize()函数获取图片宽度高度

    //php自带函数 getimagesize() $img_info = getimagesize('tomener.jpg'); echo '<pre>'; print_r($img_i ...

  4. 线段树+矩阵快速幂 Codeforces Round #373 (Div. 2) E

    http://codeforces.com/contest/719/problem/E 题目大意:给你一串数组a,a[i]表示第i个斐波那契数列,有如下操作 ①对[l,r]区间+一个val ②求出[l ...

  5. 【BZOJ2742】【HEOI2012】Akai的数学作业 [数论]

    Akai的数学作业 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 这里是广袤无垠的宇宙这里 ...

  6. [POJ1845&POJ1061]扩展欧几里得应用两例

    扩展欧几里得是用于求解不定方程.线性同余方程和乘法逆元的常用算法. 下面是代码: function Euclid(a,b:int64;var x,y:int64):int64; var t:int64 ...

  7. CVE-2016-6662 利用条件

    首先执行SET GLOBAL 需要超级用户权限,所以利用条件要么用户本身是超级用户要么用户有trigger权限,通过创建trigger,由超级用户触发SET GLOBAL. 然而MYsql有个通过fi ...

  8. django-crontab

    django-cromtab实现定时任务 参考:https://www.jianshu.com/p/1def9226158d ''' 首先安装插件:pip install django-crontab ...

  9. iptables 用法及常用模块总结

    iptables传输数据包的过程: 1. 当一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去. 2. 如果数据包就是进入本机的,它就会沿着图向下移动, ...

  10. 【SQL】索引

    1.定义 索引:一种数据结构,典型的是B-树,有键值对,键对应属性的某个值,值对应该键的存放位置. 建立索引的目的:加快查询速度 比如: SELECT * FROM Movies ; 如果有studi ...