D - 瞎捏的题(dfs/bfs/迪杰斯特拉)

DFS

ACcode

// #pragma GCC optimize (2)
// #pragma G++ optimize (2)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include<unordered_map>
#include<map>
#define endl '\n'
#define int long long
#define lowbit(x) x &(-x)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define Bookerbobo()                        \
    {                                \
        ios::sync_with_stdio(false); \
        cin.tie(0);                  \
        cout.tie(0);                 \
    }
using namespace std;
typedef long long ll;
const int N=1e6+10;
typedef pair<int,int>PII;
map<PII,int>mp;
int e[N],h[N],ne[N],cnt;
void add(int a,int b)
{
    e[cnt]=b,ne[cnt]=h[a],h[a]=cnt++;
}
int s,t;
bool st[N],ans;
void dfs(int s)
{
    if(s==t){ans=true;return;}
    for(int i=h[s];i!=-1;i=ne[i])
    {
        int j=e[i];
        if(st[j])continue;
        st[j]=true;
        dfs(j);
        if(ans)return;
        st[j]=false;
    }
    return;
}
void solve()
{
    int n,m1,m2;
    cin>>n>>m1>>m2;
    for(int i=1;i<=n;i++){h[i]=-1;}
    while(m1--)
    {
        int a,b;
        cin>>a>>b;
        mp[{a,b}]++;
    }
    while(m2--)
    {
        int a,b;
        cin>>a>>b;
        if(mp.count({a,b}))add(a,b);
    }
    ans=false;
    cin>>s>>t;
    st[s]=true;
    dfs(s);
    if(ans)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;

}
signed main()
{
    Bookerbobo();
    int T;T= 1;
    //int T;cin>>T;
    while (T--)
        solve();
    return 0;
}

BFS 

ACcode

#include <bits/stdc++.h>
#define endl '\n'
#define x first
#define y second
#define PI acos(-1)
#define int long long
#define SugarT ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

using namespace std;

const int N=2e5+10;
const int INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const double eps = 1e-6;

typedef pair<int, int> PII;

int n,m,s,t;
int dist[N];
int h[N],e[N],w[N],ne[N],idx;
bool st[N];
map<PII,int> mp;

void add(int a,int b,int c)
{
    e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}

bool bfs(int s)
{
    stack<int>sta;
    sta.push(s);
    while(sta.size())
    {
        auto p=sta.top();
        sta.pop();
        st[p]=true;
        if(p==t)return true;
        for(int i=h[p];~i;i=ne[i])
        {
            int j=e[i];
            if(st[j])continue;
            st[j]=true;
            sta.push(j);
        }
    }
    return false;
}

void solve()
{
	memset(h,-1,sizeof h);
    int n,m1,m2;
    cin >> n >> m1 >> m2;
    while(m1--)
    {
        int a,b;
        cin >> a >> b;
        mp[{a,b}]=1;
    }
	while(m2--)
	{
	    int a,b;
	    cin >> a >> b;
	    if(mp[{a,b}])
	        add(a,b,1LL);
	}
	cin >> s >> t;
	if(bfs(s))cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}

signed main()
{
	SugarT
	int T=1;
		//cin >> T;
	while(T--)
		solve();
	
	return 0;
		
}

迪杰斯特拉

ACcode

#include <bits/stdc++.h>
#define endl '\n'
#define x first
#define y second
#define PI acos(-1)
#define int long long
#define SugarT ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);

using namespace std;

const int N=2e5+10;
const int INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const double eps = 1e-6;

typedef pair<int, int> PII;

int n,m,s,t;
int dist[N];
int h[N],e[N],w[N],ne[N],idx;
bool st[N];
map<PII,int> mp;

void add(int a,int b,int c)
{
    e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}

void Dijkstra(int s,int t)
{
    memset(dist,0x3f,sizeof dist);
    dist[s]=0;
    priority_queue<PII,vector<PII>,greater<PII>> q;
    q.push({0,s});
    while(q.size())
    {
        auto t=q.top();
        q.pop();
        int dis=t.x,ver=t.y;
        if(st[ver])continue;
        st[ver]=true;
        for(int i=h[ver];i!=-1;i=ne[i])
        {
            auto j=e[i];
            if(dist[j]>dist[ver]+w[i])
            {
                dist[j]=dist[ver]+w[i];
                q.push({dist[j],j});
            }
        }
    }
    //for(int i=1;i<=n;i++)cout << dist[i] << endl;
    if(dist[t]<0x3f3f3f3f/2)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}

void solve()
{
	memset(h,-1,sizeof h);
    int n,m1,m2;
    cin >> n >> m1 >> m2;
    while(m1--)
    {
        int a,b;
        cin >> a >> b;
        mp[{a,b}]=1;
    }
	while(m2--)
	{
	    int a,b;
	    cin >> a >> b;
	    if(mp[{a,b}])
	        add(a,b,1LL);
	}
	cin >> s >> t;
	Dijkstra(s,t);
	
}

signed main()
{
	SugarT
	int T=1;
		//cin >> T;
	while(T--)
		solve();
	
	return 0;
		
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值