NOIP2017（不算是题解）

逛公园

dfs一遍判断0环就可以了，判断是否在最短路上可以正反做两次SPFA

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int T,n,m,k,p,tot,goal;
int first[2][100005],dis[2][100005],f[100005][52],sta[100005];
char tim[100005][52],ok[100005];
bool vis[100005],flag;
struct edge{
int v,w,next;
}e[2][200005];
int in()
{
int t=0;char ch=getchar();
while (ch>'9'||ch<'0') ch=getchar();
while (ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
return t;
}
{
e[0][++tot].v=y;e[0][tot].w=z;e[0][tot].next=first[0][x];first[0][x]=tot;
e[1][tot].v=x;e[1][tot].w=z;e[1][tot].next=first[1][y];first[1][y]=tot;
}
int up(int x,int y){return ((x+y)%p+p)%p;}
queue<int>q;
void SPFA(int r)
{
int now=(r?n:1);
memset(dis[r],63,sizeof(dis[r]));
dis[r][now]=0;
for (q.push(now);!q.empty();q.pop())
{
now=q.front();
for (int i=first[r][now];i;i=e[r][i].next)
if (dis[r][e[r][i].v]>dis[r][now]+e[r][i].w)
{
dis[r][e[r][i].v]=dis[r][now]+e[r][i].w;
if (!vis[e[r][i].v]) vis[e[r][i].v]=1,q.push(e[r][i].v);
}
vis[now]=0;
}
}
int dp(int x,int D)
{
if (dis[1][x]+D-goal>k) return 0;
if (tim[x][dis[1][x]+D-goal]==T) return f[x][dis[1][x]+D-goal];
tim[x][dis[1][x]+D-goal]=T;
f[x][dis[1][x]+D-goal]=(x==n);
for (int v,i=first[0][x];i;i=e[0][i].next)
{
v=e[0][i].v;
f[x][dis[1][x]+D-goal]=up(f[x][dis[1][x]+D-goal],dp(v,D+e[0][i].w));
}
return f[x][dis[1][x]+D-goal];
}
void dfs(int x)
{
ok[x]=T;
sta[++sta[0]]=x;
vis[x]=1;
for (int v,i=first[0][x];i;i=e[0][i].next)
if (e[0][i].w==0)
{
v=e[0][i].v;
if (ok[v]!=T) dfs(v);
else if (vis[v]&&dis[0][v]+dis[1][v]<=k+goal)
flag=1;
}
--sta[0];
vis[x]=0;
}
void work()
{
n=in();m=in();k=in();p=in();
tot=0;flag=0;
for (int i=1;i<=n;++i) first[0][i]=first[1][i]=0;
for (int u,v,w,i=1;i<=m;++i)
u=in(),v=in(),w=in(),
SPFA(0);
SPFA(1);
goal=dis[0][n];
for (int i=1;i<=n;++i)
if (ok[i]!=T) dfs(i);
if (flag) puts("-1");
else printf("%d\n",dp(1,0));
}
main(){for (T=in();T;--T) work();}

宝藏

#include<cstdio>
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int n,m;
int dis[13][13],f[13][1<<12],w[1<<12],bit[1<<12],p[13];
stack<int>S;
int cal(int id)
{
int t=1e9;
for (int i=1;i<=p[0];++i) t=min(t,dis[p[i]][id]);
return t;
}
main()
{
scanf("%d%d",&n,&m);
memset(dis,63,sizeof(dis));
for (int x,y,z,i=1;i<=m;++i)
scanf("%d%d%d",&x,&y,&z),
dis[x][y]=dis[y][x]=min(dis[x][y],z);
for (int i=1;i<=n;++i) dis[i][i]=0;
for (int j=1,i=1;i<=n;++i,j<<=1) bit[j]=i;
int ans=1e9,cop;
for (int s=1;s<=n;++s)
{
memset(f,63,sizeof(f));
f[0][1<<s-1]=0;
for (int k,state=(1<<s-1);state<(1<<n);++state)
if (state&(1<<s-1))
{
p[0]=0;
cop=((1<<n)-1)^state;
for (int i=cop;i;i=(cop&i-1)) S.push(i);
k=state;
for (;k;k^=(k&-k)) p[++p[0]]=bit[k&-k];
for (;!S.empty();S.pop())
k=S.top(),
w[k]=(w[k^(k&-k)]<1e9?w[k^(k&-k)]+cal(bit[k&-k]):1e9);
for (int D=0;D<n;++D)
for (int sub=cop;sub;sub=(cop&sub-1))
if (w[sub]<1e9)
f[D+1][state|sub]=min(f[D+1][state|sub],f[D][state]+w[sub]*(D+1));
}
for (int D=0;D<n;++D) ans=min(ans,f[D][(1<<n)-1]);
}
printf("%d\n",ans);
}

列队

n,m小的时候模拟，q小的时候枚举之前询问的影响，x=1时只影响第一行和第m列组成的倒”L”型，实际上变成了序列问题，线段树或平衡树随便做，这样就有50~70了

#include<cstdio>
#include<vector>
#define LL long long
#define M 300005
using namespace std;
int in()
{
int t=0;char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch>='0'&&ch<='9') t=t*10+ch-48,ch=getchar();
return t;
}
int n,m,q,cnt;
int root[M],ls[M*19],rs[M*19],del_num[M*19];
vector<LL> val[M];
int get(int &rt,int l,int r,int k)
{
if (!rt) rt=++cnt;
++del_num[rt];
if (l==r) return r;
int mid=l+r>>1,sz=mid-l+1-del_num[ls[rt]];
if (sz>=k) return get(ls[rt],l,mid,k);
else return get(rs[rt],mid+1,r,k-sz);
}
main()
{
n=in();m=in();q=in();
LL ans;
for (int x,y,t,i=1;i<=q;++i)
{
x=in();y=in();
if (y==m)
{
t=get(root[n+1],1,q+n,x);
if (t<=n) ans=1LL*t*m;
else ans=val[n+1][t-n-1];
printf("%lld\n",ans);
val[n+1].push_back(ans);
}
else
{
t=get(root[x],1,q+m,y);
if (t<m) ans=1LL*x*m+t-m;
else ans=val[x][t-m];
printf("%lld\n",ans);
val[n+1].push_back(ans);

t=get(root[n+1],1,q+n,x);
if (t<=n) ans=1LL*t*m;
else ans=val[n+1][t-n-1];
val[x].push_back(ans);
}
}
}