标签:apt 应该 scanf algorithm nod output code algo 否则
给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用。
第一行包含一个正整数n(2<=n<=200000),表示点数。
接下来n行,每行包含两个整数x[i],yi,依次表示每个点的坐标。
一个整数,即最小费用。
5
2 2
1 1
4 5
7 1
6 7
2
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define N 200010
#define inf 0x3f3f3f3f
#define pa pair<long long, int>
using namespace std;
int head[N];
long long dis[N];
int n, num;
bool vis[N];
struct node
{
int x, y, z;
}a[N];
priority_queue<pa, vector<pa>, greater<pa> > q;
bool cmp_x(node x,node y)
{
return x.x < y.x;
}
bool cmp_y(node x,node y)
{
return x.y < y.y;
}
struct edge
{
int u, v, next;
long long w;
edge(){next=-1;}
}ed[4*N];
void build(int u,int v,int w)
{
num++;
ed[num].w=w;
ed[num].v=v;
ed[num].next=head[u];
head[u]=num;
}
void SPFA()
{
memset(vis, 0, sizeof(vis));
for(int i=1; i<=n; i++) dis[i]=inf;
dis[1]=0;
q.push(make_pair(0, 1));
while(!q.empty())
{
int u=q.top().second;
q.pop();
if(vis[u])continue;
vis[u]=1;
for(int i=head[u]; i!=-1; i=ed[i].next)
{
int v=ed[i].v;
if(dis[v]>dis[u]+ed[i].w)
{
dis[v]=dis[u]+ed[i].w;
q.push(make_pair(dis[v], v));
}
}
}
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d", &n);
for (int i=1; i<=n; i++)
{
scanf("%d%d", &a[i].x, &a[i].y);
a[i].z=i;
}
sort(a+1, a+n+1, cmp_x);
for(int i=1; i<n; i++)
{
build(a[i].z, a[i+1].z, a[i+1].x-a[i].x),
build(a[i+1].z, a[i].z, a[i+1].x-a[i].x);
}
sort(a+1, a+n+1, cmp_y);
for(int i=1; i<n; i++)
{
build(a[i].z, a[i+1].z, a[i+1].y-a[i].y),
build(a[i+1].z, a[i].z, a[i+1].y-a[i].y);
}
SPFA();
printf("%lld\n", dis[n]);
return 0;
}
标签:apt 应该 scanf algorithm nod output code algo 否则
原文地址:https://www.cnblogs.com/orange-233/p/12320950.html