• 牛客2019提高D1t1 最短路


    分析

    我们发现可以按照ai从小到大排序

    边的大小就是当前的a减去前面第一个不等于它的a

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<cctype>
    #include<cmath>
    #include<cstdlib>
    #include<queue>
    #include<ctime>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    using namespace std;
    struct node {
        int x,id;
    };
    node a[100100];
    int n,m,cnt;
    struct res {
        int x,y,z;
    };
    res ans[200100];
    inline void add(int x,int y,int z){
        ans[++cnt].x=x;
        ans[cnt].y=y;
        ans[cnt].z=z;
    }
    inline bool cmp(const node x,const node y){
        return x.x<y.x;
    }
    int main(){
        int i,j,k;
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)
          scanf("%d",&a[i].x),a[i].id=i;
        sort(a+1,a+n+1,cmp);
        int la=0,pl=1;
        if(a[2].x==0){
          puts("-1");
          return 0;
        } 
        for(i=2;i<=n;i++){
          if(a[i].x-la>m){
              puts("-1");
              return 0;
          }
          while(a[i+1].x==a[i].x)add(pl,a[i].id,a[i].x-la),i++;
          add(pl,a[i].id,a[i].x-la);
          la=a[i].x;
          pl=a[i].id;
        }
        printf("%d
    ",cnt);
        for(i=1;i<=cnt;i++)
          printf("%d %d %d
    ",ans[i].x,ans[i].y,ans[i].z);
        return 0;
    }
  • 相关阅读:
    poj 1840 简单哈希
    poj 2151概率dp
    poj 3349 简单hash
    poj3274 hash
    poj 1459 最大流 Dinic模板题
    poj 3436 最大流-拆点
    poj 3020 二分图最大匹配
    poj 1094 简单拓扑排序
    poj3687 反向建图拓扑排序
    poj 3267
  • 原文地址:https://www.cnblogs.com/yzxverygood/p/11405670.html
Copyright © 2020-2023  润新知