poj 2135 Farm Tour + 费用流vector模板

本文深入探讨了深度学习与人工智能领域的最新进展,包括机器学习算法、神经网络模型、自然语言处理、计算机视觉等核心内容。通过具体案例分析,展示了这些技术在实际应用场景中的强大能力,为读者提供了深入理解与实践应用的指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Farm Tour
Time Limit: 1000MS Memory Limit: 65536K
Total 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 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

Sample Output

6

Source


从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 21
const 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 - 汇点]
//双向边,注意RE
struct 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若是单向边要为0
	edge.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_JUDGE
	freopen("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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值