uva1658 admiral
费用流。
裸的拆点最小费用流,一跑就行。
核弹预警,为何wa20多发。build函数一定要返回true。。。。。。
太可怕了
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 5000 + 10 ;
const int maxm = 200000 + 10;
const int inf = 0x3f3f3f3f;
int g[maxn],v[maxm],nex[maxm],c[maxm],f[maxm],eid;
int n,m;
int id[maxn][2],vid,S,T;
int dist[maxn],pre[maxn];
bool inque[maxn];
queue<int> q; void addedge(int a,int b,int F,int C) {
v[eid]=b; f[eid]=F; c[eid]=C; nex[eid]=g[a]; g[a]=eid++;
v[eid]=a; f[eid]=0; c[eid]=-C; nex[eid]=g[b]; g[b]=eid++;
} bool build() {
memset(id,0,sizeof(id));
if(!(scanf("%d%d",&n,&m) && n && m)) return false;
memset(g,-1,sizeof(g)); eid=0; vid=0;
for(int i=1;i<=n;i++) {
id[i][0]=++vid;
id[i][1]=++vid;
addedge(id[i][0],id[i][1],1,0);
}
for(int i=1,u,v,w;i<=m;i++) {
scanf("%d%d%d",&u,&v,&w);
if(v!=1 && u!=n) addedge(id[u][1],id[v][0],1,w);
}
S=++vid;
T=++vid;
addedge(S,id[1][1],2,0);
addedge(id[n][0],T,2,0);
return true;
} bool SPFA() {
memset(dist,0x7f,sizeof(dist));
memset(inque,0,sizeof(inque));
memset(pre,0,sizeof(pre));
dist[S]=0;
q.push(S);
inque[S]=1;
int u;
while(!q.empty()) {
u=q.front(); q.pop();
inque[u]=0;
for(int i=g[u];~i;i=nex[i])
if(f[i] && dist[v[i]]>c[i]+dist[u]) {
dist[v[i]]=c[i]+dist[u];
pre[v[i]]=i;
if(!inque[v[i]]) {
q.push(v[i]);
inque[v[i]]=1;
}
}
}
return dist[T]<inf;
} int augment() {
int aug=inf,res=0;
for(int i=T;i!=S;i=v[pre[i]^1]) aug=min(aug,f[pre[i]]);
for(int i=T;i!=S;i=v[pre[i]^1]) {
f[pre[i]]-=aug;
f[pre[i]^1]+=aug;
res+=c[pre[i]]*aug;
}
return res;
} void solve() {
int res=0;
while(SPFA())
res+=augment();
printf("%d\n",res);
} int main() {
while(build()) solve();
return 0;
}
uva1658 admiral的更多相关文章
- UVA1658 Admiral 拆点法解决结点容量(路径不能有公共点,容量为1的时候) 最小费用最大流
/** 题目:UVA1658 Admiral 链接:https://vjudge.net/problem/UVA-1658 题意:lrj入门经典P375 求从s到t的两条不相交(除了s和t外,没有公共 ...
- UVa1658 Admiral(拆点法+最小费用流)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51253 [思路] 固定流量的最小费用流. 拆点,将u拆分成u1和u ...
- UVA1658:Admiral
题意:给定一个有向带权图,求两条不相交(无公共点)的路径且路径权值之和最小,路径由1到v 题解:这题的关键就在于每个点只能走一遍,于是我们想到以边换点的思想,用边来代替点,怎么代替呢? 把i拆成i和i ...
- 【HDU 6171】Admiral(搜索+剪枝)
多校10 1001 HDU 6171 Admiral 题意 目标状态是第i行有i+1个i数字(i=0-5)共6行.给你初始状态,数字0可以交换上一行最近的两个和下一行最近的两个.求20步以内到目标状态 ...
- uva 1658 Admiral (最小费最大流)
uva 1658 Admiral 题目大意:在图中找出两条没有交集的线路,要求这两条线路的费用最小. 解题思路:还是拆点建图的问题. 首先每一个点都要拆成两个点.比如a点拆成a->a'.起点和终 ...
- UVA - 1658 Admiral
3. C - Admiral 题意:给定v(3<=v<=1000)个节点,e(3<=e<=10000)条边的又向加权图,求1->v的两条不相交的路径,使得权和最小. 思路 ...
- UVALive - 6266 Admiral 费用流
UVALive - 6266 Admiral 题意:找两条完全不相交不重复的路使得权值和最小. 思路:比赛的时候时间都卡在D题了,没有仔细的想这题,其实还是很简单的,将每个点拆开,连一条容量为1,费用 ...
- Admiral(双向BFS + Hash)
Problem Description Suppose that you are an admiral of a famous naval troop. Our naval forces have g ...
- UVa(1658),Admiral,海军上将,拆点,MCMF
题目链接:https://uva.onlinejudge.org/external/16/1658.pdf 题意:求1到N的两条路(不能相交),距离和最小. 分析: 第一次做拆点,有点意思.刚开始一直 ...
随机推荐
- 《疯狂的android讲义第3版》读书笔记
第一章.开始启程,你的第一行android代码 1.android系统架构: 1)linux内核层:为底层硬件提供驱动,如显示驱动.音频驱动.照相机驱动.蓝牙驱动.Wifi驱动.电源管理等 2)系统运 ...
- Inside Microsoft SQL Server 2008: T-SQL Querying 读书笔记1
(5)SELECT (5-2) DISTINCT (5-3)TOP(<top_specifications>) (5-1)<select_list> (1)FRO ...
- C# 学习之旅(3) --- 会说话的简易计算器
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- C语言多线程编程
HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize, LPTHREAD_START_ROUT ...
- (转)Qt Model/View 学习笔记 (五)——View 类
Qt Model/View 学习笔记 (五) View 类 概念 在model/view架构中,view从model中获得数据项然后显示给用户.数据显示的方式不必与model提供的表示方式相同,可以与 ...
- 深入浅出话XAML-学习笔记
第一章 XAML是什么? 1.1XAML之前 *设计师的设计更不上程序逻辑的变化 *程序员未能完全实现设计师提供的效果图 *效果图与程序功能不能完全匹配 *从效果图到软件UI的转化耗费很多时间 1.2 ...
- TabControl控件
private void Form1_Load(object sender, EventArgs e) { #region 显示样式 tabControl1.ImageList = imageList ...
- 安装Nuget上常用的包的命令
起因: Nuget图形化操作界面各种卡顿,或者有时干脆就连不上了.所以用命令还是很必须的. 常用命令: 安装 Entity Framework : PM> Install-Package Ent ...
- EXTJS 资料 combo 点一次触发一次
{ xtype: 'combo', id: 'ApplyToModel', name: 'ApplyToModel', store: comStoreApplyToModel, valueField: ...
- Eat the Trees hdu 1693
Problem DescriptionMost of us know that in the game called DotA(Defense of the Ancient), Pudge is a ...