Meeting point-2

Meeting point-2
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

It has been ten years since TJU-ACM established. And in this year all the retired TJU-ACMers want to get together to celebrate the tenth anniversary. Because the retired TJU-ACMers may live in different places around the world, it may be hard to find out where to celebrate this meeting in order to minimize the sum travel time of all the retired TJU-ACMers. 

It's an opportunity to show yourself in front of your predecessors!

There is an infinite integer grid at which N retired TJU-ACMers have their houses on. They decide to unite at a common meeting place, which is someone's house. From any given cell, all 8 adjacent cells are reachable in 1 unit of time.

Eg: (x,y) can be reached from (x-1,y), (x+1,y), (x, y-1), (x, y+1), (x-1,y+1), (x-1,y-1), (x+1,y+1), (x+1,y-1).

Finding a common meeting place which minimizes the sum of the travel time of all the retired TJU-ACMers.
 

Input

The first line is an integer T represents there are T test cases. (0<T <=10)
For each test case, the first line is an integer n represents there are n retired TJU-ACMers. (0<n<=100000), the following n lines each contains two integers x, y coordinate of the i-th TJU-ACMer. (-10^9 <= x,y <= 10^9)
 

Output

For each test case, output the minimal sum of travel times.
 

Sample Input

     
     
4 6 -4 -1 -1 -2 2 -4 0 2 0 3 5 -2 6 0 0 2 0 -5 -2 2 -2 -1 2 4 0 5 -5 1 -1 3 3 1 3 -1 1 -1 10 -1 -1 -3 2 -4 4 5 2 5 -4 3 -1 4 3 -1 -2 3 4 -2 2
 

Sample Output

     
     
20 15 14 38

Hint

In the first case, the meeting point is (0,2); the second is (0,0), the third is (1,-1) and the last is (-1,-1)
 
 


在做完前一题的基础之上,后一题就稍微容易了一点。这题和上一题基本思路一样,就把哈夫曼距离改为切夫比雪距离就OK。先在有8个放向,如果把之前的那些点都旋转45度就原则上就变为一样的哈夫曼距离了。

简单的说就是Chebyshev距离,利用(x, y) -> (x - y, x+y) 转换为Manhattan距离,就和4311一样的代码了。


代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include<algorithm>
#define M 100005
using namespace std;
#define LL __int64
struct point {
    LL x;
    LL y;
    int id;
} p[M];

bool cmpx(point e1,point e2) {
    return e1.x < e2.x;
}
bool cmpy(point e1,point e2) {
    return e1.y < e2.y;
}
LL temp[M],sumx[M],sumy[M];
int main() {
    int cse;
    cin>>cse;
    while(cse--) {
        int n;
        scanf("%d",&n);
        for(int i=1; i<=n; i++) {
            scanf("%I64d%I64d",&p[i].x,&p[i].y);
            LL tt=p[i].x;
            p[i].x+=p[i].y;
            p[i].y-=tt;
            p[i].id = i;
        }


        memset(sumx,0,sizeof(sumx));
        memset(sumy,0,sizeof(sumy));

        sort(p+1, p+n+1, cmpx);

        temp[0] = 0;
        for(int i=1; i<=n; i++)
            temp[i] = temp[i-1] + p[i].x;
        for(int i=1; i<=n; i++)
            sumx[ p[i].id ] = temp[n]  - 2*temp[i-1] + (2*i-n-2)*p[i].x;

        sort(p+1, p+n+1, cmpy);

        temp[0] = 0;
        for(int i=1; i<=n; i++)
            temp[i] = temp[i-1] + p[i].y;
        for(int i=1; i<=n; i++)
            sumy[ p[i].id ] = temp[n] -  2*temp[i-1] + (2*i-n-2)*p[i].y;


        LL ans = sumx[1] + sumy[1];
        for(int i=2; i<=n; i++) {
            if(ans > sumx[i]+sumy[i])
                ans = (sumx[i]+sumy[i]);

        }

        printf("%I64d\n",ans>>1);

    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是最大流的C++实现代码,使用了scanf函数: ```cpp #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int MAXN = 205; const int INF = 0x3f3f3f3f; struct Edge { int from, to, cap, nxt; } edge[MAXN * MAXN]; int head[MAXN], cnt = 1; int n, m, s, t; inline void add_edge(int u, int v, int c) { edge[++cnt].from = u; edge[cnt].to = v; edge[cnt].cap = c; edge[cnt].nxt = head[u]; head[u] = cnt; } inline void add(int u, int v, int c) { add_edge(u, v, c); add_edge(v, u, 0); } int dep[MAXN], cur[MAXN]; inline bool bfs() { memset(dep, 0, sizeof(dep)); queue<int> q; q.push(s); dep[s] = 1; cur[s] = head[s]; while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; i; i = edge[i].nxt) { int v = edge[i].to; if (!dep[v] && edge[i].cap) { dep[v] = dep[u] + 1; cur[v] = head[v]; if (v == t) return true; q.push(v); } } } return false; } inline int dfs(int u, int flow) { if (u == t) return flow; int used = 0; for (int i = cur[u]; i && flow > used; i = edge[i].nxt) { cur[u] = i; int v = edge[i].to; if (dep[v] == dep[u] + 1 && edge[i].cap) { int d = dfs(v, min(flow - used, edge[i].cap)); if (!d) dep[v] = 0; used += d; edge[i].cap -= d; edge[i ^ 1].cap += d; } } return used; } inline int Dinic() { int maxflow = 0; while (bfs()) maxflow += dfs(s, INF); return maxflow; } inline void init() { memset(head, 0, sizeof(head)); cnt = 1; s = 1, t = m; } int main() { while (scanf("%d%d", &n, &m) == 2) { init(); for (int i = 1; i <= n; i++) { int u, v, c; scanf("%d%d%d", &u, &v, &c); add(u, v, c); } printf("%d\n", Dinic()); } return 0; } ``` 其中,add_edge函数用于添加边,bfs函数用于进行BFS遍历,dfs函数用于进行DFS遍历,Dinic函数用于计算最大流,init函数用于初始化。在main函数中,我们首先调用init函数初始化,然后读入每条边的信息,调用add函数将其添加到图中,最后调用Dinic函数计算最大流并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值