您的位置:首页 > 其它

poj 2135 最小费用最大流模板题

2015-08-24 18:27 411 查看
Farm Tour

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 13459Accepted: 5098
Description

When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= N <= 1000) fields numbered 1..N, the first of which contains his house and the Nth of which contains the big barn. A total M (1 <= M <= 10000) paths that connect
the fields in various ways. Each path connects two different fields and has a nonzero length smaller than 35,000.

To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. Later, he returns (potentially through some fields) back to his house again.

He wants his tour to be as short as possible, however he doesn't want to walk on any given path more than once. Calculate the shortest tour possible. FJ is sure that some tour exists for any given farm.
Input

* Line 1: Two space-separated integers: N and M.

* Lines 2..M+1: Three space-separated integers that define a path: The starting field, the end field, and the path's length.

Output

A single line containing the length of the shortest tour.

Sample Input
4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

Sample Output
6

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;

#pragma comment(linker,"/STACK:102400000,102400000") /// kuo zhan
#define clr(s,x) memset(s,x,sizeof(s))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lowbit(x) (x&(-x))
#define PB push_back
#define For(i,a,b) for(int i=a;i<b;i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)

typedef long long               LL;
typedef unsigned int            uint;
typedef unsigned long long      ULL;
typedef vector<int>             vint;
typedef vector<string>          vstring;

void RI (int& x){
x = 0;
char c = getchar ();
while (c == ' '||c == '\n')    c = getchar ();
bool flag = 1;
if (c == '-'){
flag = 0;
c = getchar ();
}
while (c >= '0' && c <= '9'){
x = x * 10 + c - '0';
c = getchar ();
}
if (!flag)    x = -x;
}
void RII (int& x, int& y){RI (x), RI (y);}
void RIII (int& x, int& y, int& z){RI (x), RI (y), RI (z);}

const double PI  = acos(-1.0);
const int maxn   = 1e4 + 100;
const int maxm   = 1e3 + 100;
const LL mod     = 1e9 + 7;
const double eps = 1e-9;
const int INF    = 0x7fffffff;

/************************************END DEFINE*********************************************/

struct Side{
int u,v,cap,w;
int nxt;
}side[maxn*6];
int head[maxm],pre[maxm],dis[maxm];
bool vis[maxm];
int min_cost;
int cnt;

void init(){
cnt = 0;
clr(head,-1);
}

void add_side(int u,int v,int cap,int w){
side[cnt].u = u;
side[cnt].v = v;
side[cnt].cap = cap;
side[cnt].w = w;
side[cnt].nxt = head[u];
head[u] = cnt++;

side[cnt].u = v;
side[cnt].v = u;
side[cnt].cap = 0;
side[cnt].w = -w;
side[cnt].nxt = head[v];
head[v] = cnt++;
}

int spfa(int s,int t){
queue<int>q;
while(!q.empty())q.pop();
FOR(i,s,t){
vis[i] = 0;
dis[i] = INF;
pre[i] = -1;
}
dis[s] = 0;
q.push(s);
vis[s] = 1;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = 0;
for(int i=head[u];i!=-1;i=side[i].nxt){
int v = side[i].v;
if(side[i].cap && dis[u]+side[i].w < dis[v]){
dis[v] = dis[u]+side[i].w;
pre[v] = i;
if(!vis[v]){
vis[v] = 1;
q.push(v);
}
}
}
}
if(pre[t] == -1)return 0;
return 1;
}

void End(int t){
int mid = INF;
int k = pre[t];
while(k!=-1){
mid = min(mid,side[k].cap);
k = pre[side[k].u];
}
k = pre[t];
while(k!=-1){
side[k].cap-=mid;
side[k^1].cap+=mid;
k=pre[side[k].u];
}
min_cost += dis[t]*mid;
}

int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
init();
while(m--){
int u,v,w;
RIII(u,v,w);
add_side(u,v,1,w);
add_side(v,u,1,w);
}
add_side(0,1,2,0);
add_side(n,n+1,2,0);
int s = 0,t = n+1;
min_cost = 0;
while(spfa(s,t))End(t);
printf("%d\n",min_cost);
}
return 0;
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: