题解:
大致题意为给定3e3的无向图,已经给定1e5组非法行走路线(ai,bi,ci),问最少走几条路从1走到n,并输出路线
由于点只有3e3,所以可以记录前驱是pre,当前是x为小节点,是否跑过,来跑一遍bfs 这样就不会出现队列元素过多的情况出现。
代码:
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
//#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define PB push_back
#define MP make_pair
#define INF 1073741824
#define inf 1152921504606846976
#define pi 3.14159265358979323846
//#pragma comment(linker,"/STACK:10240000,10240000")
//mt19937 rand_(time(0));
const int N=3e5+7,M=2e6;
const long long mod=1e9+7;
inline int read(){int ret=0;char ch=getchar();bool f=1;for(;!isdigit(ch);ch=getchar()) f^=!(ch^'-');for(;isdigit(ch);ch=getchar()) ret=(ret<<1)+(ret<<3)+ch-48;return f?ret:-ret;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}//逆元
int head[N],NEXT[M],ver[M],tot;void link(int u,int v){ver[++tot]=v;NEXT[tot]=head[u];head[u]=tot;}
bool vis[3500][3500];
pair<int,int> que[3500][3500];
set<pair<pair<int,int>,int> >s;
stack<int>Stack;
int n,fi,se;
bool bfs(){
queue<pair<int,int> >q;
q.push(MP(1,0));
vis[1][0]=true;
while(!q.empty()){
pair<int,int> now=q.front();
//cout<<now.first<<' '<<now.second<<endl;
q.pop();
int x=now.first;
for(int i=head[x];i;i=NEXT[i]){
int y=ver[i];
if(vis[y][x]) continue;
if(s.find(MP(MP(now.second,now.first),y))!=s.end()) continue;
q.push(MP(y,x));
que[y][x]=now;
vis[y][x]=true;
if(y==n){
fi=y,se=x;
return true;
}
}
}
return false;
}
int main(){
//freopen("1.txt","r",stdin);
//ios::sync_with_stdio(false);
int m,k;
int u,v,w;
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=m;i++){
scanf("%d%d",&u,&v);
link(u,v);
link(v,u);
}
for(int i=1;i<=k;i++){
scanf("%d%d%d",&u,&v,&w);
s.insert(MP(MP(u,v),w));
}
if(bfs()){
while(fi!=1&&se!=0){
Stack.push(fi);
pair<int,int>tmp=que[fi][se];
fi=tmp.first;
se=tmp.second;
}
Stack.push(1);
printf("%d\n",Stack.size()-1);
while(!Stack.empty()){
printf("%d ",Stack.top());
Stack.pop();
}
}
else {
puts("-1");
}
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}