bzoj4152 [AMPPZ2014]The Captain

4152: [AMPPZ2014]The Captain

Time Limit: 20 Sec  Memory Limit: 256 MB
Submit: 1293  Solved: 502
[Submit][Status][Discuss]

5
2 2
1 1
4 5
7 1
6 7

2

Source

不过这道题卡spfa,用dijkstra就是稳定的O(nlogn)了，不会超时.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;

const int maxn = 200010;
const long long inf = 1000000000000000;

struct node
{
int x,y,id;
}e[200010];

struct node2
{
int x;
long long len;
};

bool operator < (node2 a,node2 b)
{
return a.len > b.len;
}

int n,head[maxn],to[maxn * 4],nextt[maxn * 4],tot = 1,w[maxn * 4],vis[maxn * 4];
priority_queue <node2> q;
long long d[maxn];

bool cmp1(node a,node b)
{
return a.x < b.x;
}

bool cmp2(node a,node b)
{
return a.y < b.y;
}

void add(int x,int y,int z)
{
w[tot] = z;
to[tot] = y;
}

void dijkstra()
{
for (int i = 1; i <= n; i++)
d[i] = inf;
d[1] = 0;
node2 temp;
temp.len = 0;
temp.x = 1;
q.push(temp);
while (!q.empty())
{
while (!q.empty() && vis[q.top().x])
q.pop();
if (q.empty())
return;
node2 u = q.top();
q.pop();
int x = u.x;
long long len = u.len;
vis[x] = 1;
for (int i = head[x]; i;i = nextt[i])
{
int v = to[i];
if (d[v] > d[x] + w[i])
{
d[v] = d[x] + w[i];
node2 temp;
temp.x = v;
temp.len = d[v];
q.push(temp);
}
}
}
}

int main()
{
scanf("%d",&n);
for (int i = 1; i <= n; i++)
{
scanf("%d%d",&e[i].x,&e[i].y);
e[i].id = i;
}
sort(e + 1,e + 1 + n,cmp1);
for (int i = 1; i < n; i++)
{
int t = min(abs(e[i].x - e[i + 1].x),abs(e[i].y - e[i + 1].y));
}
sort(e + 1,e + 1 + n,cmp2);
for (int i = 1; i < n; i++)
{
int t = min(abs(e[i].x - e[i + 1].x),abs(e[i].y - e[i + 1].y));
}