标签:add += typename har jks pen put push bit
由题面显然可得,所求即最短路树。
所以跑出最短路树,计数,输出方案即可。
#include<bits/stdc++.h>
using namespace std;
template <typename Tp>
void read(Tp &x){
x=0;char ch=1;int fh;
while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();
if(ch=='-') ch=getchar(),fh=-1;
else fh=1;
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
x*=fh;
}
const int maxn=200007;
const int maxm=400007;
const int INF=0x3f3f3f3f;
int n,m,k;
int Head[maxn],to[maxm],Next[maxm],tot,w[maxm];
void add(int x,int y){
to[++tot]=y,Next[tot]=Head[x],Head[x]=tot,w[tot]=1;
}
int dis[maxn];
bool vis[maxn];
priority_queue< pair<int,int> > q;
#define pii(x,y) make_pair(x,y)
void dijkstra(){
memset(dis,0x3f,sizeof(dis));
q.push(pii(0,1));dis[1]=0;
while(q.size()){
int x=q.top().second;q.pop();
if(vis[x]) continue;vis[x]=1;
for(int i=Head[x];i;i=Next[i]){
int y=to[i];
if(dis[y]>dis[x]+w[i]){
dis[y]=dis[x]+w[i];
q.push(pii(-dis[y],y));
}
}
}
}
int ans=1;
vector<int>g[maxn];
int val[maxn];
void build(){
for(int x=1;x<=n;x++){
for(int i=Head[x];i;i=Next[i]){
int y=to[i];
if(dis[y]==dis[x]+w[i]){
val[y]++;
g[y].push_back((i+1)>>1);
}
}
}
for(int i=1;i<=n;i++){
if(val[i]) ans=ans*val[i];
if(ans>=k){
ans=k;return;
}
}
}
bool v[maxm];
int md;
void dfs(int x){
if(x==n+1){
for(int i=1;i<=tot;i+=2) printf("%d",v[(i+1)>>1]);
puts("");++md;
if(md==ans) exit(0);return;
}
for(int i=0;i<g[x].size();i++){
v[g[x][i]]=1;dfs(x+1);v[g[x][i]]=0;
}
}
int main(){
read(n);read(m);read(k);
for(int i=1,x,y;i<=m;i++){
read(x);read(y);
add(x,y);add(y,x);
}
dijkstra();build();
printf("%d\n",ans);
dfs(2);
return 0;
}
CF1005F Berland and the Shortest Paths 最短路树计数
标签:add += typename har jks pen put push bit
原文地址:https://www.cnblogs.com/liubainian/p/11808905.html