poj 2135 Farm Tour + 费用流vector模板

来源:互联网 发布:送女生生日礼物 知乎 编辑:程序博客网 时间:2024/05/16 18:20

Farm Tour
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11260 Accepted: 4174

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 51 2 12 3 13 4 11 3 22 4 2

Sample Output

6

Source

USACO 2003 February Green

从1走到n,再从n走回1,每条路只能走一次,求最短路。

费用流水题,主要用来试板,果然还是喜欢vector啊

#include <iostream>#include <algorithm>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#include <bitset>#include <functional>#include <sstream>#include <iomanip>#include <cmath>#include <cstdlib>#include <ctime>//#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;#define INF 1e9#define MAXN 21const int maxn = 1005;//#define mod 1000000007#define eps 1e-7#define pi 3.1415926535897932384626433#define rep(i,n) for(int i=0;i<n;i++)#define rep1(i,n) for(int i=1;i<=n;i++)#define scan(n) scanf("%d",&n)#define scanll(n) scanf("%I64d",&n)#define scan2(n,m) scanf("%d%d",&n,&m)#define scans(s) scanf("%s",s);#define ini(a) memset(a,0,sizeof(a))#define out(n) printf("%d\n",n)//ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b);}using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define N 1220#define M 50000#define inf (1<<29)//注意 点标必须是 [0 - 汇点]//双向边,注意REstruct Edge{    int from, to, flow, cap, cost;};vector<int> G[maxn];vector<Edge> edge;//int head[N], edgenum;void add(int u,int v,int cap,int cost){//网络流要加反向弧    Edge E={u, v, 0, cap, cost};edge.push_back(E);    Edge E2={v, u, 0, 0, -cost}; //这里的cap若是单向边要为0edge.push_back(E2);int m = edge.size();G[u].push_back(m-2);G[v].push_back(m-1);}int D[N], P[N], A[N];bool inq[N];bool BeintmanFord(int s, int t, int &flow, int &cost){    for(int i=0;i<=t;i++) D[i] = inf;    memset(inq, 0, sizeof(inq));    D[s]=0;  inq[s]=1; P[s]=0; A[s]=inf;    queue<int> Q;    Q.push( s );    while( !Q.empty()){        int u = Q.front(); Q.pop();        inq[u]=0;for(int i=0; i<(int)G[u].size(); i++){            Edge &E = edge[G[u][i]];            if(E.cap > E.flow && D[E.to] > D[u] +E.cost){                D[E.to] = D[u] + E.cost ;                P[E.to] = G[u][i];                A[E.to] = min(A[u], E.cap - E.flow);                if(!inq[E.to]) Q.push(E.to) , inq[E.to] = 1;            }        }    }    if(D[t] == inf) return false;    flow += A[t];    cost += D[t] * A[t];    int u = t;    while(u != s){        edge[P[u]].flow += A[t];        edge[P[u]^1].flow -= A[t];        u = edge[P[u]].from;    }    return true;}int flow;int Mincost(int s,int t){//返回最小费用    flow = 0 ; int cost = 0;    while(BeintmanFord(s, t, flow, cost));    return cost;}void init(int n){edge.clear();for(int i = 0;i <= n; i++) G[i].clear();}int n,m;int main(){#ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);// freopen("out.txt","w",stdout);#endif  while(~scanf("%d%d",&n,&m)){init(n+1);int s = 0,t = n + 1;int u,v,c;rep1(i,m){scanf("%d%d%d",&u,&v,&c);add(u,v,1,c);add(v,u,1,c);}add(s,1,2,0);add(n,t,2,0);int ans = Mincost(s,t);cout<<ans<<endl;}return 0;}



0 0
原创粉丝点击