POJ 2677 旅行商问题 双调dp或者费用流

Tour

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3408   Accepted: 1513

Description

John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must determine the shortest closed tour that connects his destinations. Each destination
is represented by a point in the plane pi = < xi,yi >. John uses the following strategy: he starts from the leftmost point, then he goes strictly left to right to the rightmost point, and then he goes strictly right back to the starting point. It is known
that the points have distinct x-coordinates.

Write a program that, given a set of n points in the plane, computes the shortest closed tour that connects the points according to John‘s strategy.

Input

The program input is from a text file. Each data set in the file stands for a particular set of points. For each set of points the data set contains the number of points, and the point coordinates in ascending order of the x coordinate.
White spaces can occur freely in input. The input data are correct.

Output

For each set of data, your program should print the result to the standard output from the beginning of a line. The tour length, a floating-point number with two fractional digits, represents the result. An input/output sample
is in the table below. Here there are two data sets. The first one contains 3 points specified by their x and y coordinates. The second point, for example, has the x coordinate 2, and the y coordinate 3. The result for each data set is the tour length, (6.47
for the first data set in the given example).

Sample Input

3
1 1
2 3
3 1
4
1 1
2 3
3 1
4 2

Sample Output

一旅行商从左向右走到最右边,然后再返回原来出发点的最短路径。

两种做法,第一种dp,dp[i][j]表示以i,j结尾的两条不相交的路径假设i一定大于j,i有两种选择,与i-1相连,不与i-1相连,然后dp

代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <algorithm>
#include <stdlib.h>
using namespace std;
#define ll int
#define N 1005
#define inf 100000000
struct node{
	double x, y;
	bool operator<(const node&a)const{
		if(a.x==x)return a.y>y;
		return a.x>x;
	}
}p[N];
double Dis(node a, node b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}
int n;
double dis[N][N],dp[N][N];
int main(){
    ll i, j, u, v;
    while(~scanf("%d",&n)){//dp[i][j]表示以i,j为结尾的两条不相交的路径
        for(i=1;i<=n;i++)scanf("%lf %lf",&p[i].x,&p[i].y);
		sort(p+1,p+n+1);
		for(i=1;i<=n;i++)for(j=1;j<=n;j++)dis[i][j] = Dis(p[i],p[j]), dp[i][j] = inf;

		dp[1][1] = 0;
		for(i=2;i<=n;i++)
		{
			for(j = 1;j < i; j++)
			{
				dp[i][j] = min(dp[i-1][j]+dis[i][i-1], dp[i][j]);//i不与i-1相连,
				dp[i][i-1] = min(dp[i-1][j]+dis[j][i], dp[i][i-1]);//i与i-1相连。
			}
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++)cout<<dp[i][j]<<" ";
			cout<<endl;
		}
		printf("%.2lf\n",dp[n][n-1]+dis[n][n-1]);
    }
    return 0;
}

费用流,把每一个点拆点,中间连流量为1,费用为负无穷的边,代表该点必须选择,两两之间连流量为1,费用为两点距离的边,起点,终点连边,流量为1,费用为0.

代表可以增广两次。

代码:

/* ***********************************************
Author :_rabbit
Created Time :2014/5/17 9:42:51
File Name :6.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 1001000
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=4010;
const int maxm=200000;
struct Edge{
    int next,to,cap;
	double cost;
    Edge(int _next=0,int _to=0,int _cap=0,double _cost=0){
        next=_next;to=_to;cap=_cap;cost=_cost;
    }
}edge[maxm];
int head[maxn],vis[maxn],pre[maxn],n,tol;
double dis[maxn];
void addedge(int u,int v,int cap,double cost){
    edge[tol]=Edge(head[u],v,cap,cost);head[u]=tol++;
    edge[tol]=Edge(head[v],u,0,-cost);head[v]=tol++;
}
bool spfa(int s,int t){
    queue<int> q;
    for(int i=0;i<=n;i++)
        dis[i]=INF,vis[i]=0,pre[i]=-1;
    dis[s]=0;vis[s]=1;q.push(s);
    while(!q.empty()){
        int u=q.front();q.pop();vis[u]=0;
	//	cout<<"u="<<u<<" "<<dis[u]<<endl;
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            if(edge[i].cap>0&&dis[v]>dis[u]+edge[i].cost){
                dis[v]=dis[u]+edge[i].cost;
                pre[v]=i;
                if(!vis[v])vis[v]=1,q.push(v);
            }
        }
    }
    if(pre[t]==-1)return 0;
    return 1;
}
void fun(int s,int t,int &flow,double &cost){
    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)MIN=edge[i].cap;
        for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
            edge[i].cap-=MIN,edge[i^1].cap+=MIN,cost+=edge[i].cost*MIN;
        flow+=MIN;
    }
}
struct Point{
	double x,y;
}pp[10000];
double dist(Point a,Point b){
	double ss=a.x-b.x;
	double tt=a.y-b.y;
	return sqrt(ss*ss+tt*tt);
}
int main(){
	int m;
//	freopen("data.out","w",stdout);
	while(cin>>m){
		memset(head,-1,sizeof(head));tol=0;
		for(int i=1;i<=m;i++)cin>>pp[i].x>>pp[i].y;
		for(int i=1;i<=m;i++){
			for(int j=i+1;j<=m;j++){
				double dd=dist(pp[i],pp[j]);
				addedge(i+m,j,1,dd);
			}
			addedge(i,i+m,1,-INF);
		}
		addedge(1,m+1,1,0);
		addedge(m,2*m,1,0);
		n=2*m;
		int flow;double cost;
		fun(1,2*m,flow,cost);
		printf("%.2lf\n",cost+m*INF);
	}
	return 0;
}

POJ 2677 旅行商问题 双调dp或者费用流

时间: 2024-10-14 18:04:57

POJ 2677 旅行商问题 双调dp或者费用流的相关文章

POJ 2677 Tour 双调旅行商 dp, double+费用流

题目链接:点击打开链接 题意:给定二维平面上的n个点 从最左端点到最右端点(只能向右移动) 再返回到到最右端点(只能向左移动,且走过的点不能再走) 问最短路. 费用流: 为了达到遍历每个点的效果 把i点拆成 i && i+n 在i ->i+n 建一条费用为 -inf 的边,流量为1 这样跑最短路时必然会经过这条边,以此达到遍历的效果. dp :点击打开链接 对于i点 :只能跟一个点相连 -- 1.跟 i-1点相连 2.不跟i-1相连 用dp[i][j] 表示两个线头为 i 和 j 的

POJ 2195 Going Home(网络流-费用流)

Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17777   Accepted: 9059 Description On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertical

poj 3422 Kaka&#39;s Matrix Travels 费用流

题意: 给一个n*n的矩阵,每次从左上角走到右下角并取走其中的数,求走k次能取到的最大和. 分析: 费用流边的容量有限制的作用,费用有求和的作用,对于每个点只能取一次,容易想到把这个点拆成两个点并连上容量为1,费用为该点数的边.但明显有的流要"跳过"这个点,如何处理呢?可以加一条容量为无穷,费用为0的边,这样不参加这点费用计算的流就可以"跳过"这个点了. 代码: //poj 3422 //sep9 #include <iostream> #include

POJ 2516 Minimum Cost(网络流之费用流)

题目地址:POJ 2516 我晕啊...这题一上来就想到了对每种货物分开求..但是马上就放弃了..感觉这样求50次费用流太耗时..后来就果断拆点,拆了好长时间,一直TLE..即使降到了2600个点也TLE..然后又想起了这个分开求的方法,又突然觉得100个点的费用流几乎不费什么时间..最多也只是求50次而已,还是可以试试的..于是一试居然还真过了... 说到这里,思路应该已经知道了吧.就是对每种货物分开求,因为每种货物是相互独立的.每一次的建图思路就是: 源点与供应商连边,流量权值为供应商这种货

POJ 2677(双调旅行商问题&lt;bictonicTSP&gt;

Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3470 Accepted: 1545 Description John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must dete

POJ 2135 Farm Tour(网络流之费用流)

题目地址:POJ 2135 来回走一遍可以看成从源点到汇点走两遍.将每个点的流量设为1,就可以保证每条边不重复.然后跑一次费用流就行了.当流量到了2之后停止,输出此时的费用. #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <qu

UVA 1347(POJ 2677)Tour(双调欧几里得旅行商问题)

Tour                 Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu Description John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John mus

[ACM] POJ 2677 Tour (动态规划,双调欧几里得旅行商问题)

Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3585   Accepted: 1597 Description John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in