正题
有<=m个不同的图,把权值从小到大来排序,每次往边集中加入相等边权的边。
首先可以求出利用前面的边集,走步的邻接矩阵是什么样子的,并且保证前面没有走到过n。
现在可以在当前的图上继续走,看一下在走的过程中是否有到过n。
维护一个邻接矩阵G[i],使其等于,其中加法表示或,乘法表示邻接矩阵转移,E为当前边集。
G[i]的处理方法可以考虑
若存在G[i],使得这个邻接矩阵可以从1走到n,那么答案就在
范围中。
当然存在依然是无法确定的,我们需要准确的知道答案,倍增即可,每次若*G[i]不可以使得邻接矩阵从1走到n,就乘上,因为可以保证乘上
都不能使得邻接矩阵从1走到n,最后加1即可,易证必定满足。
如果不存在或者存在但是所需最小步数,那么说明答案还需要走上下一个图,以检查是否有更优的答案,在这里给出一组数据,希望过了的同学依然可以测测:
8 8
1 2 0
2 3 0
3 4 0
4 5 0
5 6 0
6 7 0
7 8 0
6 8 5
Right Answer:6
Simple Answer:7
附上代码以便参考:
#include<bits/stdc++.h>
using namespace std;
const int N=151;
int n,m;
struct edge{
int x,y,c;
bool operator<(const edge q)const{
return c<q.c;
}
}s[N];
struct Matrix{
bool G[N][N];
void clear(){memset(G,false,sizeof(G));}
Matrix operator*(const Matrix&A)const{
Matrix F;F.clear();
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++) if(G[i][k])
for(int j=1;j<=n;j++)
F.G[i][j]|=A.G[k][j];
return F;
}
Matrix operator+(const Matrix&A)const{
Matrix F;F.clear();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
F.G[i][j]=G[i][j]|A.G[i][j];
return F;
}
}p,e,H[40],now,F[40],op;
int main(){
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++) scanf("%d %d %d",&s[i].x,&s[i].y,&s[i].c);
sort(s+1,s+1+m);
int id=1;
if(s[1].c!=0){printf("Impossible");return 0;}
p.G[1][1]=1;
s[m+1].c=2e9;
while(id<=m){
int tmp=id,T=0;
bool tf=false;
while(s[id].c==s[id+1].c) id++;
for(int i=tmp;i<=id;i++) e.G[s[i].x][s[i].y]=1;
H[0]=e;for(int i=1;i<=n;i++) H[0].G[i][i]=1;F[0]=e;
while((1ll<<T)<s[id+1].c-s[id].c) T++;
for(int i=0;i<=T;i++){
if((p*H[i]).G[1][n]) {T=i,tf=true;break;}
H[i+1]=H[i]*H[i];F[i+1]=F[i]*F[i];
}
if(tf){
int ans=1;op=p;
for(int i=T-1;i>=0;i--) if(!(op*H[i]).G[1][n]) op=op*F[i],ans+=(1<<i);
if(s[id].c+ans<=s[id+1].c){
printf("%d\n",s[id].c+ans);
return 0;
}
}
int fuck=s[id+1].c-s[id].c;
for(int i=T;i>=0;i--) if(fuck-(1<<i)>=0) p=p*F[i],fuck-=1<<i;
id++;
}
printf("Impossible");
}
323

被折叠的 条评论
为什么被折叠?



