bfs80分,求调
查看原帖
bfs80分,求调
1528563
lyt_tcsn楼主2025/8/3 16:13
#include<bits/stdc++.h>
using namespace std;
int n,m,d,u,v;
struct edge{
	int v,k;
	bool f;
};
vector<edge> g[105];
int vis[105];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int a,b,k;
		cin>>a>>b>>k;
		g[a].push_back({b,k,false});
		g[b].push_back({a,k,false});
	}
	cin>>d;
	for(int i=1;i<=d;i++){
		int a,b;
		cin>>a>>b;
		for(edge& e:g[a]){
			if(e.v==b){e.f=true;break;}
		}
		for(edge& e:g[b]){
			if(e.v==a){e.f=true;break;}
		}
	}
	cin>>u>>v;
	queue<pair<int,int> > q;
	vis[u]=1;
	q.push(make_pair(u,0));
	int ans=0x3f3f3f3f;
	while(!q.empty()){
		auto t=q.front();
		q.pop();
		if(t.first==v){
			ans=min(ans,t.second);
			continue;
		}
		for(edge& e:g[t.first]){
			if(!vis[e.v]){
				vis[e.v]=1;
				q.push(make_pair(e.v,e.f*e.k+t.second));
			}
		}
	}
	cout<<ans;
	return 0;
}
2025/8/3 16:13
加载中...