您的位置:首页 > 其它

POJ 2135.Farm Tour 最小费用最大流

2017-09-13 02:41 387 查看
传送门:http://poj.org/problem?id=2135

Farm Tour

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17795 Accepted: 6892
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


第二道练手水题。注意无向图每条边要入两次图。然后1条边等于要占4个edge[],所以edge[]一定要开到40000+

#include <iostream>
#include<stdio.h>
#include<queue>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;

struct EDGE
{
int from,to,cost,capacity,next;
}edge[40025];                //注意这里要开到40000!
int edgenum=0;
int head[1005];
int last[1005];

void newedge(int from,int to,int cost,int cap)
{
EDGE TMP={from,to,cost,cap,head[from]};
head[from]=edgenum;
edge[edgenum++]=TMP;
TMP={to,from,-cost,0,head[to]};
head[to]=edgenum;
edge[edgenum++]=TMP;
}

bool spfa(int s,int t,int n)
{

int
4000
np;
queue<int> Q;
Q.push(s);
int dist[1005];
int mark[1005];
memset(mark,0,sizeof(mark));
memset(last,-1,sizeof(dist));
memset(dist,-1,sizeof(dist));
dist[s]=0;
while(!Q.empty())
{
np=Q.front();
Q.pop();
mark[np]=0;
for(int i=head[np];i!=-1;i=edge[i].next)
{
if((dist[edge[i].to]==-1||dist[edge[i].to]>dist[np]+edge[i].cost)&&edge[i].capacity)
{
// cout<<np<<' '<<edge[i].to<<endl;
dist[edge[i].to]=dist[np]+edge[i].cost;
last[edge[i].to]=i;
if(mark[edge[i].to]==0)
{
mark[edge[i].to]=1;
Q.push(edge[i].to);
}
}
}
}

if(dist[t]==-1) return false;
return true;
}

int mfmc(int s,int t,int n)
{
int sum=0;
int minn;
while(spfa(s,t,n))
{
minn=999999;
int pre=t;
while(pre!=s)
{
//cout<<edge[last[pre]].from<<' '<<edge[last[pre]].capacity<<endl;
minn=min(minn,edge[last[pre]].capacity);
sum+=edge[last[pre]].cost;      //  我看大家这里用的都是cost*minn,可能有些题会给单位流量的COST把?
pre=edge[last[pre]^1].to;
}
pre=t;
while(pre!=s)
{
edge[last[pre]].capacity-=minn;
edge[last[pre]^1].capacity+=minn;
pre=edge[last[pre]^1].to;
}
}
return sum;

}

int main()
{
int n,m;
cin>>n>>m;
int s=0;
int t=n+1;
memset(head,-1,sizeof(head));
newedge(s,1,0,2);
newedge(n,t,0,2);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d %d %d",&x,&y,&z);
newedge(x,y,z,1);                         //  入两次
newedge(y,x,z,1);
}
cout<<mfmc(s,t,n+2);

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