codeforces59E Shortest Path (bfs)

15 篇文章 0 订阅
6 篇文章 0 订阅

题解:

大致题意为给定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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值