提交记录 4613


用户 题目 状态 得分 用时 内存 语言 代码长度
wang3312362136 noi18a. 【NOI2018】归程 Accepted 100 1.264 s 68808 KB C++ 4.21 KB
提交时间 评测时间
2018-07-26 11:55:24 2020-07-31 23:09:02
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>

const int maxn=200000;
const int maxm=400000;
const int inf=0x3f3f3f3f;

int read()
{
  int x=0,f=1;
  char ch=getchar();
  while((ch<'0')||(ch>'9'))
    {
      if(ch=='-')
        {
          f=-f;
        }
      ch=getchar();
    }
  while((ch>='0')&&(ch<='9'))
    {
      x=x*10+ch-'0';
      ch=getchar();
    }
  return x*f;
}

namespace dsu
{
  int fa[maxn*2+10];

  int clear()
  {
    memset(fa,0,sizeof fa);
    return 0;
  }

  int find(int x)
  {
    return fa[x]?fa[x]=find(fa[x]):x;
  }
}

struct edge
{
  int u,v,l;

  edge(int u_=0,int v_=0,int l_=0)
  {
    u=u_;
    v=v_;
    l=l_;
  }

  bool operator <(const edge &other) const
  {
    return l<other.l;
  }
};

int n,m;

namespace tree
{
  int pre[maxn*2+10],now[maxn*2+10],son[maxn*2+10],tot;
  int val[maxn+10],fa[maxn*2+10][20],dist[maxn*2+10];

  int clear()
  {
    tot=0;
    memset(now,0,sizeof now);
    memset(val,0,sizeof val);
    return 0;
  }

  int ins(int a,int b)
  {
    pre[++tot]=now[a];
    now[a]=tot;
    son[tot]=b;
    return 0;
  }

  int getval()
  {
    for(int i=1; i<=18; ++i)
      {
        for(int j=1; j<=2*n+1; ++j)
          {
            fa[j][i]=fa[fa[j][i-1]][i-1];
          }
      }
    return 0;
  }

  int dfs(int u,int f)
  {
    fa[u][0]=f;
    if(u>=n)
      {
        return 0;
      }
    int j=now[u];
    dist[u]=inf;
    while(j)
      {
        int v=son[j];
        dfs(v,u);
        dist[u]=std::min(dist[u],dist[v]);
        j=pre[j];
      }
    return 0;
  }

  int getdist(int v,int p)
  {
    v=v+n-1;
    if(val[fa[v][0]]<=p)
      {
        return dist[v];
      }
    v=fa[v][0];
    for(int i=18; i>=0; --i)
      {
        if(fa[v][i]&&(val[fa[v][i]]>p))
          {
            v=fa[v][i];
          }
      }
    return dist[v];
  }
}

struct data
{
  int dist,id;

  data(int dist_=0,int id_=0)
  {
    dist=dist_;
    id=id_;
  }

  bool operator <(const data &other) const
  {
    return dist>other.dist;
  }
};

namespace graph
{
  int pre[maxm*2+10],now[maxn+10],son[maxm*2+10],tot;
  int dist[maxn+10],val[maxm*2+10],cnt,vis[maxn+10];
  edge e[maxm+10];
  std::priority_queue<data> h;

  int clear()
  {
    tot=cnt=0;
    memset(now,0,sizeof now);
    return 0;
  }

  int ins(int a,int b,int c)
  {
    pre[++tot]=now[a];
    now[a]=tot;
    son[tot]=b;
    val[tot]=c;
    return 0;
  }

  int add(int a,int b,int c,int d)
  {
    ins(a,b,c);
    ins(b,a,c);
    e[++cnt]=edge(a,b,d);
    return 0;
  }

  int kruskal()
  {
    std::sort(e+1,e+cnt+1);
    int need=n-1;
    for(int i=cnt; i; --i)
      {
        int fu=dsu::find(e[i].u+n-1),fv=dsu::find(e[i].v+n-1);
        if(fu!=fv)
          {
            dsu::fa[fu]=dsu::fa[fv]=need;
            tree::ins(need,fu);
            tree::ins(need,fv);
            tree::val[need]=e[i].l;
            --need;
            if(!need)
              {
                break;
              }
          }
      }
    return 0;
  }

  int dijkstra(int s)
  {
    memset(dist,63,sizeof dist);
    dist[s]=0;
    memset(vis,0,sizeof vis);
    h.push(data(0,s));
    while(!h.empty())
      {
        data w=h.top();
        h.pop();
        if(vis[w.id])
          {
            continue;
          }
        vis[w.id]=1;
        int j=now[w.id];
        while(j)
          {
            int v=son[j];
            if(dist[w.id]+val[j]<dist[v])
              {
                dist[v]=dist[w.id]+val[j];
                h.push(data(dist[v],v));
              }
            j=pre[j];
          }
      }
    for(int i=1; i<=n; ++i)
      {
        tree::dist[i+n-1]=dist[i];
      }
    return 0;
  }
}

int main()
{
  int t=read();
  while(t--)
    {
      dsu::clear();
      tree::clear();
      graph::clear();
      n=read();
      m=read();
      while(m--)
        {
          int u=read(),v=read(),l=read(),h=read();
          graph::add(u,v,l,h);
        }
      graph::kruskal();
      graph::dijkstra(1);
      tree::dfs(1,0);
      tree::getval();
      int q=read(),k=read(),s=read();
      int lastans=0;
      while(q--)
        {
          int v=read(),p=read();
          v=(v+lastans*k-1)%n+1;
          p=(p+lastans*k)%(s+1);
          lastans=tree::getdist(v,p);
          printf("%d\n",lastans);
        }
    }
  return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #11.494 ms10 MB + 744 KBAcceptedScore: 5

Testcase #21.515 ms10 MB + 748 KBAcceptedScore: 5

Testcase #31.622 ms10 MB + 764 KBAcceptedScore: 5

Testcase #41.727 ms10 MB + 780 KBAcceptedScore: 5

Testcase #54.614 ms11 MB + 136 KBAcceptedScore: 5

Testcase #6599.988 ms57 MB + 1016 KBAcceptedScore: 5

Testcase #74.224 ms11 MB + 104 KBAcceptedScore: 5

Testcase #84.225 ms11 MB + 108 KBAcceptedScore: 5

Testcase #94.227 ms11 MB + 108 KBAcceptedScore: 5

Testcase #10507.737 ms55 MB + 240 KBAcceptedScore: 5

Testcase #11508.849 ms55 MB + 248 KBAcceptedScore: 5

Testcase #12738.52 ms63 MB + 740 KBAcceptedScore: 5

Testcase #13738.436 ms63 MB + 720 KBAcceptedScore: 5

Testcase #14738.819 ms63 MB + 748 KBAcceptedScore: 5

Testcase #155.284 ms11 MB + 168 KBAcceptedScore: 5

Testcase #165.282 ms11 MB + 172 KBAcceptedScore: 5

Testcase #17738.72 ms63 MB + 732 KBAcceptedScore: 5

Testcase #18737.451 ms63 MB + 712 KBAcceptedScore: 5

Testcase #191.26 s67 MB + 176 KBAcceptedScore: 5

Testcase #201.264 s67 MB + 200 KBAcceptedScore: 5


Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:06:39 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠