zoukankan      html  css  js  c++  java
  • 4152: [AMPPZ2014]The Captain

    4152: [AMPPZ2014]The Captain

    Time Limit: 20 Sec  Memory Limit: 256 MB
    Submit: 1561  Solved: 620
    [Submit][Status][Discuss]

    Description

    给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用。

    Input

    第一行包含一个正整数n(2<=n<=200000),表示点数。
    接下来n行,每行包含两个整数x[i],y[i](0<=x[i],y[i]<=10^9),依次表示每个点的坐标。
     
     

    Output

    一个整数,即最小费用。

    Sample Input

    5
    2 2
    1 1
    4 5
    7 1
    6 7

    Sample Output

    2
     

    code

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<queue>
     6 #define mp(a,b) make_pair(a,b)
     7 #define pa pair<long long ,int>
     8 using namespace std;
     9 
    10 typedef long long LL;
    11 const int N = 200100;
    12 const LL INF = 1e18;
    13 
    14 struct Node {
    15     int x,y,id;
    16 }d[N];
    17 int head[N],L,R,tot,n;
    18 bool vis[N];
    19 struct Edge{
    20     int to,nxt,w;
    21 }e[1000100];
    22 long long dis[N];
    23 priority_queue< pa,vector<pa>,greater<pa> >q;
    24 
    25 inline char nc() {
    26     static char buf[100000],*p1 = buf,*p2 = buf;
    27     return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
    28 }
    29 inline int read() {
    30     int x = 0,f = 1;char ch = nc();
    31     for (; ch<'0'||ch>'9'; ch = nc()) if (ch=='-') f = -1;
    32     for (; ch>='0'&&ch<='9'; ch = nc()) x = x * 10 + ch - '0';
    33     return x * f;
    34 }
    35 bool cmp1(Node a,Node b) {
    36     return a.x < b.x;
    37 }
    38 bool cmp2(Node a,Node b) {
    39     return a.y < b.y;
    40 }
    41 void add_edge(int u,int v,int w) {
    42     e[++tot].to = v;e[tot].w = w;e[tot].nxt = head[u];head[u] = tot;
    43     e[++tot].to = u;e[tot].w = w;e[tot].nxt = head[v];head[v] = tot;
    44 }
    45 void dij() {
    46     for (int i=1; i<=n; ++i) dis[i] = INF;
    47     L = 1;R = 0;
    48     q.push(mp(0,1));
    49     dis[1] = 0;
    50     while (!q.empty()) {
    51         pa x = q.top();q.pop();
    52         int u = x.second;
    53         if (vis[u]) continue;vis[u] = true;
    54         for (int i=head[u]; i; i=e[i].nxt) {
    55             int v = e[i].to,w = e[i].w;;
    56             if (dis[v]>dis[u]+w) {
    57                 dis[v] = dis[u] + w;
    58                 q.push(mp(dis[v],v));
    59             }
    60         }
    61     }
    62 }
    63 int main() {
    64     freopen("1.txt","r",stdin);
    65     n = read();
    66     for (int i=1; i<=n; ++i) 
    67         d[i].x = read(),d[i].y = read(),d[i].id = i;
    68     sort(d+1,d+n+1,cmp1);
    69     for (int i=1; i<n; ++i) 
    70         add_edge(d[i].id,d[i+1].id,d[i+1].x-d[i].x);
    71     sort(d+1,d+n+1,cmp2);
    72     for (int i=1; i<n; ++i) 
    73         add_edge(d[i].id,d[i+1].id,d[i+1].y-d[i].y);
    74     dij();
    75     printf("%d",dis[n]);
    76     return 0;
    77 }
    View Code

    spfa被卡了QwQ

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<queue>
     6 
     7 using namespace std;
     8 
     9 typedef long long LL;
    10 const int N = 200100;
    11 const LL INF = 1e18;
    12 
    13 struct Node {
    14     int x,y,id;
    15 }d[N];
    16 int head[N],L,R,tot,n;
    17 bool vis[N];
    18 struct Edge{
    19     int to,nxt,w;
    20 }e[1000100];
    21 long long dis[N];
    22 queue<int>q;
    23 
    24 inline char nc() {
    25     static char buf[100000],*p1 = buf,*p2 = buf;
    26     return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
    27 }
    28 inline int read() {
    29     int x = 0,f = 1;char ch = nc();
    30     for (; ch<'0'||ch>'9'; ch = nc()) if (ch=='-') f = -1;
    31     for (; ch>='0'&&ch<='9'; ch = nc()) x = x * 10 + ch - '0';
    32     return x * f;
    33 }
    34 bool cmp1(Node a,Node b) {
    35     return a.x < b.x;
    36 }
    37 bool cmp2(Node a,Node b) {
    38     return a.y < b.y;
    39 }
    40 void add_edge(int u,int v,int w) {
    41     e[++tot].to = v;e[tot].w = w;e[tot].nxt = head[u];head[u] = tot;
    42     e[++tot].to = u;e[tot].w = w;e[tot].nxt = head[v];head[v] = tot;
    43 }
    44 void spfa() {
    45     for (int i=1; i<=n; ++i) dis[i] = INF;
    46     L = 1;R = 0;
    47     q.push(1);
    48     dis[1] = 0;
    49     vis[1] = true;
    50     while (!q.empty()) {
    51         int u = q.front();q.pop();
    52         for (int i=head[u]; i; i=e[i].nxt) {
    53             int v = e[i].to,w = e[i].w;;
    54             if (dis[v]>dis[u]+w) {
    55                 dis[v] = dis[u] + w;
    56                 if (!vis[v])q.push(v),vis[v] = true;
    57             }
    58         }
    59         vis[u] = false;
    60     }
    61 }
    62 int main() {
    63     n = read();
    64     for (int i=1; i<=n; ++i) 
    65         d[i].x = read(),d[i].y = read(),d[i].id = i;
    66     sort(d+1,d+n+1,cmp1);
    67     for (int i=1; i<n; ++i) 
    68         add_edge(d[i].id,d[i+1].id,d[i+1].x-d[i].x);
    69     sort(d+1,d+n+1,cmp2);
    70     for (int i=1; i<n; ++i) 
    71         add_edge(d[i].id,d[i+1].id,d[i+1].y-d[i].y);
    72     spfa();
    73     printf("%d",dis[n]);
    74     return 0;
    75 }
    View Code
     
     
  • 相关阅读:
    笨方法学python中执行argv提示ValueError: not enough values to unpack (expected 4, got 1)
    VMware workstation安装
    Redis bigkey分析
    MySQL drop table 影响及过程
    MySQL 大表硬连接删除
    ES elasticsearch 各种查询
    ES elasticsearch 各种聚合
    ES elasticsearch 聚合统计
    ES elasticsearch 实现 count单字段,分组取前多少位,以地理位置中心进行统计
    MySQL行溢出、varchar最多能存多少字符
  • 原文地址:https://www.cnblogs.com/mjtcn/p/8484585.html
Copyright © 2011-2022 走看看