1108: [POI2007]天然气管道Gaz
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 410 Solved: 211
[Submit][Status]
Description
Mary
试图控制成都的天然气市场。专家已经标示出了最好的天然气井和中转站在成都的地图。现在需要将中转站和天然气井连接起来。每个中转站必须被连接到正好一个
钻油井,反之亦然。
Mary特别指名,建设的天然气管道必须从某个天然气井开始,向南或者向东建设。Mary想知道怎么连接每个天然气井和中转站,使得需要的天然气管道的总
长度最小。
Input
输入文件的第一行为一个正整数
n(2<=n<=50000),表示天然气井的数量(中转站的数量与之相等)。接下来n行,每行两个整数xi和yi(0&
lt;=xi,yi<=100000),表示天然气井的坐标。向东走则x坐标增加,向北走则y坐标增加。接下来n行,每行两个数xj’和
yj’(0<=xj’,yj'<=100000),表示中转站的坐标。
Output
输出文件第一行包含一个数,表示最短的连接管道长度。
Sample Input
3
3 5
1 2
4 3
6 3
5 2
2 1
Sample Output
9
HINT
Source
题解:
乍一看题目,卧操,这不是KM吗?n=50000?。。。。。。。。然后果断膜拜题解。。。
发现漏看了两句话:
1.天然井必须向南或向东建设管道
2.是曼哈顿距离而不是欧几里得距离
1s变水题。。。
不管每个点与谁相连,它的横纵坐标总会被作为减数的一部分,所以把两部分的x,y都加起来减一下取个绝对值即可。
代码:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 50000
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n;
ll sx[],sy[];
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
for0(i,)
for1(j,n)
sx[i]+=read(),sy[i]+=read();
printf("%lld\n",abs(sx[]-sx[])+abs(sy[]-sy[]));
return ;
}