vjudge传送门[here]


  题目大意:给一个有(3≤v≤1000)个点e(3≤e≤10000)条边的有向加权图,求1~v的两条不相交(除了起点和终点外没有公共点)的路径,使权值和最小。

  正解是吧2到v-1的每个点拆成两个点,中间连一条容量为1,费用为0的边,然后求1到v的流量为2的最小费用流就行了。

Code

 /**
* Uva
* Problem#1658
* Accepted
*/
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef bool boolean;
#define INF 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && ~x);
if(x == -){
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} ///map template starts
typedef class Edge{
public:
int end;
int next;
int cap;
int flow;
int cost;
Edge(const int end = , const int next = , const int cap = , const int flow = , const int cost = ):end(end), next(next), cap(cap), flow(flow), cost(cost){}
}Edge; typedef class MapManager{
public:
int ce;
int *h;
Edge *edge;
MapManager(){}
MapManager(int points, int limit):ce(){
h = new int[(const int)(points + )];
edge = new Edge[(const int)(limit + )];
memset(h, , sizeof(int) * (points + ));
}
inline void addEdge(int from, int end, int cap, int flow, int cost){
edge[++ce] = Edge(end, h[from], cap, flow, cost);
h[from] = ce;
}
inline void addDoubleEdge(int from, int end, int cap, int cost){
addEdge(from, end, cap, , cost);
addEdge(end, from, cap, cap, -cost);
}
Edge& operator [](int pos){
return edge[pos];
}
inline int reverse(int pos){
return (pos & ) ? (pos + ) : (pos - );
}
inline void clean(){
delete[] h;
delete[] edge;
ce = ;
}
}MapManager;
#define m_begin(g, i) (g).h[(i)]
/// map template ends int n, m;
MapManager g; inline boolean init(){
if(!readInteger(n)) return false;
readInteger(m);
g = MapManager(n * , m * + n * );
for(int i = , a, b, w; i <= m; i++){
readInteger(a);
readInteger(b);
readInteger(w);
g.addDoubleEdge(a + n, b, , w);
}
for(int i = ; i <= n; i++){ //拆点
g.addDoubleEdge(i, i + n, , );
}
return true;
} int* dis;
boolean* visited;
int* last;
int* laste;
int* mflow;
int cost;
int s, t;
queue<int> que;
int sizee; inline void spfa(){
memset(dis, 0x7f, sizeof(int) * (sizee));
memset(visited, false, sizeof(boolean) * (sizee));
que.push(s);
last[s] = ;
dis[s] = ;
laste[s] = ;
mflow[s] = INF;
visited[s] = true;
while(!que.empty()){
int e = que.front();
que.pop();
visited[e] = false;
for(int i = m_begin(g, e); i != ; i = g[i].next){
int &eu = g[i].end;
if(dis[e] + g[i].cost < dis[eu] && g[i].flow < g[i].cap){
dis[eu] = dis[e] + g[i].cost;
last[eu] = e;
laste[eu] = i;
mflow[eu] = min(g[i].cap - g[i].flow, mflow[e]);
if(!visited[eu] && eu != t){
que.push(eu);
visited[eu] = true;
}
}
}
}
for(int i = t; i != s; i = last[i]){
g[laste[i]].flow += mflow[t];
g[g.reverse(laste[i])].flow -= mflow[t];
cost += mflow[t] * g[laste[i]].cost;
}
} inline void minCostFlow(){
s = + n, t = n, sizee = * n + ;
dis = new int[(const int)(sizee)];
visited = new boolean[(const int)(sizee)];
last = new int[(const int)(sizee)];
laste = new int[(const int)(sizee)];
mflow = new int[(const int)(sizee)];
spfa();
spfa();
} inline void solve(){
cost = ;
minCostFlow();
printf("%d\n", cost);
g.clean();
} int main(){
while(init()){
solve();
}
return ;
}

最新文章

  1. java多线程同步,等待,唤醒
  2. 敏捷开发与jira之项目现状
  3. openlayers3 画扇形
  4. 利用PBFunc在Powerbuilder中支付宝当面付功能
  5. C++基础(2)
  6. DWZ的选择带回功能无法带回第一个value中的值
  7. python 购物车和三级菜单
  8. javascript_04 数据类型
  9. Spring框架学习之第5节
  10. 安卓开发24:FrameLayout布局
  11. Java Web 错误排查
  12. Hibernate学习---单表查询
  13. C# 多线程中经常访问同一资源可能造成什么问题?
  14. python中如何删除列表中的所有元素
  15. python使用关键字爬取url
  16. 关于 私有变量的访问问题【 java python]
  17. Js组件的一些写法
  18. pageadmin CMS网站建设教程:如何修改用户密码?
  19. 更改Request Parameters中的值
  20. 混沌数学之Chua&#39;s circuit(蔡氏电路)

热门文章

  1. python3学习笔记(3)_dict-set
  2. webstorm添加调试nodejs
  3. AndroidStudio 3.3+ ButterKnife R2 红名问题
  4. 【Python】easygui小甲鱼
  5. memcached-session-manager 教程实现session共享
  6. zkSNARK 零知识验证
  7. qsv转mp4
  8. mysql 备份脚本
  9. [LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈
  10. pythonon ddt数据驱动二(json, yaml 驱动)