其他分享
首页 > 其他分享> > 【BZOJ2118】墨墨的等式(同余最短路)

【BZOJ2118】墨墨的等式(同余最短路)

作者:互联网

题意:

 

 

 

 思路:From https://www.cnblogs.com/GavinZheng/p/11709153.html#4421510

写的1e9,int范围的

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long ll;
  4 typedef unsigned int uint;
  5 typedef unsigned long long ull;
  6 typedef long double ld;
  7 typedef pair<int,int> PII;
  8 typedef pair<ll,ll> Pll;
  9 typedef vector<int> VI;
 10 typedef vector<PII> VII;
 11 typedef pair<ll,ll>P;
 12 #define N  500010
 13 #define M  6000010
 14 #define INF 1e9
 15 #define fi first
 16 #define se second
 17 #define MP make_pair
 18 #define pb push_back
 19 #define pi acos(-1)
 20 #define mem(a,b) memset(a,b,sizeof(a))
 21 #define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
 22 #define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
 23 #define lowbit(x) x&(-x)
 24 #define Rand (rand()*(1<<16)+rand())
 25 #define id(x) ((x)<=B?(x):m-n/(x)+1)
 26 #define ls p<<1
 27 #define rs p<<1|1
 28 #define fors(i) for(auto i:e[x]) if(i!=p)
 29 
 30 const int MOD=1e8+7,inv2=(MOD+1)/2;
 31       int p=1e4+7;
 32       double eps=1e-8;
 33       int dx[4]={-1,1,0,0};
 34       int dy[4]={0,0,-1,1};
 35 
 36 ll dis[N];
 37 int head[N],vet[M],nxt[M],len[M],a[N],vis[N],mn,n,tot;
 38 
 39 int read()
 40 {
 41    int v=0,f=1;
 42    char c=getchar();
 43    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
 44    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
 45    return v*f;
 46 }
 47 
 48 ll readll()
 49 {
 50    ll v=0,f=1;
 51    char c=getchar();
 52    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
 53    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
 54    return v*f;
 55 }
 56 
 57 void add(int a,int b,int c)
 58 {
 59     nxt[++tot]=head[a];
 60     vet[tot]=b;
 61     len[tot]=c;
 62     head[a]=tot;
 63 }
 64 
 65 void build()
 66 {
 67     rep(i,0,mn-1) head[i]=0;
 68     tot=0;
 69     rep(i,0,mn-1)
 70      rep(j,1,n) add(i,(i+a[j])%mn,a[j]);
 71 }
 72 
 73 void dijk()
 74 {
 75     priority_queue<pair<ll,int> >q;
 76     mem(vis,0);
 77     mem(dis,0x3f);
 78     q.push(MP(0,0)); dis[0]=0;
 79     while(!q.empty())
 80     {
 81         int u=q.top().se;
 82         q.pop();
 83         if(vis[u]) continue;
 84         vis[u]=1;
 85         int e=head[u];
 86         while(e)
 87         {
 88             int v=vet[e];
 89             if(dis[u]+len[e]<dis[v])
 90             {
 91                 dis[v]=dis[u]+len[e];
 92                 q.push(MP(-dis[v],v));
 93             }
 94             e=nxt[e];
 95         }
 96     }
 97 }
 98 
 99 int main()
100 {
101     n=read();
102     ll L=readll(),R=readll();
103     L--;
104     mn=INF;
105     int flag=0;
106     rep(i,1,n)
107     {
108         a[i]=read();
109         if(a[i])
110         {
111             mn=min(mn,a[i]);
112             flag=1;
113         }
114     }
115     if(mn==INF)
116     {
117         printf("0\n");
118         return 0;
119     }
120     build();
121     dijk();
122     if(flag) dis[0]=0;
123      else dis[0]=mn;
124     ll ans=0;
125     rep(i,0,mn-1)
126     {
127         if(R>=dis[i]) ans+=((R-dis[i])/mn)+1;
128         if(L>=dis[i]) ans-=((L-dis[i])/mn)+1;
129     }
130     printf("%lld\n",ans);
131     return 0;
132 }

 

标签:typedef,int,dis,long,同余,mem,墨墨,BZOJ2118,define
来源: https://www.cnblogs.com/myx12345/p/11877249.html