1 条题解
-
0

// 限制最短路 BFS 算法 O(N+M+KlogK) #include<bits/stdc++.h> using namespace std; const int N=3005; int n,m,k; vector<int> e[N]; map<pair<pair<int,int>,int>,int> limit; int p[N][N]; pair<int,int> t; stack<int> path; void bfs(){ t={0,0}; queue<pair<int,int> > q; q.push({1,1}); while(!q.empty()){ auto [a,b]=q.front(); q.pop(); if(b==n){t={a,b}; break;} //记录最短路的最后一条边的端点 for(int c:e[b]){ if(p[b][c] || limit[{{a,b},c}])continue; //去掉重复的路径和限制的路径 p[b][c]=a; //记录路径b到c的父亲是a q.push({b,c}); } } } int main(){ scanf("%d%d%d",&n,&m,&k); for(int i=1,u,v; i<=m; i++){ scanf("%d%d",&u,&v); e[u].push_back(v); e[v].push_back(u); } for(int i=1,a,b,c; i<=k; i++){ scanf("%d%d%d",&a,&b,&c); limit[{{a,b},c}]++; } bfs(); if(t.first==0){puts("-1"); return 0;} while(t.second!=1){ path.push(t.second); //把路径边的右端点压入栈 t={p[t.first][t.second],t.first}; //回溯路径边的端点 } path.push(1); printf("%d\n",path.size()-1); while(!path.empty()) printf("%d ",path.top()),path.pop(); return 0; }
- 1
信息
- ID
- 12510
- 时间
- 3000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 3
- 已通过
- 2
- 上传者