poj 2135 Farm Tour(最小费用最大流,好题)
2016-09-02 22:22
375 查看
题目链接
Farm Tour
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
Sample Output
Source
USACO 2003 February Green
题意:
给定一个无向图,要从1点到n点再返回1点,每条边最多走一次,问最短需要走多远。
题解:
可以将问题转化为从1号顶点到N号顶点的两条没有公共边的路径,创立一个开始结点,从开始结点向结点1加一条流量为2的边,对题目给定的边连流量为1,费用为路径长度的边,注意这里建图的时候要连双向边!!!原因想一想就知道了,虽然将问题转化成了求两条路径,但仍然得是双向边!
Farm Tour
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15088 | Accepted: 5771 |
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
USACO 2003 February Green
题意:
给定一个无向图,要从1点到n点再返回1点,每条边最多走一次,问最短需要走多远。
题解:
可以将问题转化为从1号顶点到N号顶点的两条没有公共边的路径,创立一个开始结点,从开始结点向结点1加一条流量为2的边,对题目给定的边连流量为1,费用为路径长度的边,注意这里建图的时候要连双向边!!!原因想一想就知道了,虽然将问题转化成了求两条路径,但仍然得是双向边!
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<queue> using namespace std; const int MAXN = 10000+100; const int MAXM = 200000+100; const int INF = 0x3f3f3f3f; struct Edge { int to,next,cap,flow,cost; }edge[MAXM]; int head[MAXN],tol; int pre[MAXN],dis[MAXN]; bool vis[MAXN]; int N;//节点总个数,节点编号从0~N-1 void init(int n) { N = n; tol = 0; memset(head,-1,sizeof(head)); } void addedge(int u,int v,int cap,int cost) { edge[tol].to = v; edge[tol].cap = cap; edge[tol].cost = cost; edge[tol].flow = 0; edge[tol].next = head[u]; head[u] = tol++; edge[tol].to = u; edge[tol].cap = 0; edge[tol].cost = -cost; edge[tol].flow = 0; edge[tol].next = head[v]; head[v] = tol++; } bool spfa(int s,int t) { queue<int>q; for(int i = 0;i < N;i++) { dis[i] = INF; vis[i] = false; pre[i] = -1; } dis[s] = 0; vis[s] = true; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for(int i = head[u]; i != -1;i = edge[i].next) { int v = edge[i].to; if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost ) { dis[v] = dis[u] + edge[i].cost; pre[v] = i; if(!vis[v]) { vis[v] = true; q.push(v); } } } } if(pre[t] == -1)return false; else return true; } //返回的是最大流,cost存的是最小费用 int minCostMaxflow(int s,int t,int &cost) { int flow = 0; cost = 0; while(spfa(s,t)) { int Min = INF; for(int i = pre[t];i != -1;i = pre[edge[i^1].to]) { if(Min > edge[i].cap - edge[i].flow) Min = edge[i].cap - edge[i].flow; } for(int i = pre[t];i != -1;i = pre[edge[i^1].to]) { edge[i].flow += Min; edge[i^1].flow -= Min; cost += edge[i].cost * Min; } flow += Min; } return flow; } int main() { int n,m; scanf("%d%d",&n,&m); { int start=0,end=n; init(n+1); while(m--) { int u,v,w; scanf("%d%d%d",&u,&v,&w); addedge(u,v,1,w); addedge(v,u,1,w); } addedge(start,1,2,0); int cost=0; minCostMaxflow(start,end,cost); printf("%d\n",cost); } }
相关文章推荐
- POJ 2135 Farm Tour(最小费用最大流)
- POJ 2135 Farm Tour (最小费用最大流)
- poj 2135 Farm Tour(最小费用最大流)
- poj 2135 Farm Tour--最小费用最大流--邻接表--构图的时候注意退边--以及退边的算法
- 【POJ】2135 Farm Tour 最小费用最大流
- poj 2135 Farm Tour 【无向图最小费用最大流】
- POJ 2135 Farm Tour (最小费用最大流)
- poj--2135--Farm Tour(最小费用最大流)
- POJ 2135 Farm Tour 最小费用最大流
- POJ 2135 Farm Tour (最小费用最大流)
- poj_2135 Farm Tour(最小费用最大流)
- poj--2135--Farm Tour(最小费用最大流)
- POJ2135 Farm Tour(最小费用最大流)
- poj 2135 Farm Tour (最小费用最大流)
- POJ 2135 Farm Tour (网络流,最小费用最大流)
- poj 2135 Farm Tour 【最小费用最大流】
- POJ 2135 - Farm Tour 无向图的最小费用最大流加边时注意..要分两次加.不能一次完成..
- POJ 2135 Farm Tour(最小费用最大流 模板题)
- poj 2135 Farm Tour(最小费用最大流)
- POJ 2135 Farm Tour【最小费用最大流】