谁能告诉我哪里错了?
查看原帖
谁能告诉我哪里错了?
1412140
_0x3f_楼主2025/1/23 10:26

rt,40pts,求调。

#include<bits/stdc++.h>
using namespace std;
#define int long long
struct node{
	int x,y,w;
	bool operator<(const node &T)const{
		if(x==T.x&&y==T.y){
			return w>T.w;
		}
		return w<T.w;
	} 
};
int n,m,maxn,minn=1e18,D[505][505],s,t,fa[505],dis[505],Father[505];
bool vis[505];
vector<node>G;
vector<pair<int,int>>v[505];
int Find(int x){
	if(x==fa[x]){
		return x;
	}
	return fa[x]=Find(fa[x]);
}
void JO(int x,int y){
	x=Find(x),y=Find(y);
	if(x!=y){
		fa[x]=y;
	}
}
void Dijkstra(int Begin){
	fill(dis,dis+n+1,1e18);
	fill(vis,vis+n+1,false);
	priority_queue<pair<int,int>>q;
	dis[Begin]=0;
	q.push({dis[Begin],Begin});
	while(q.size()){
		auto p=q.top();
		q.pop();
		if(!vis[p.second]){
			vis[p.second]=true;
			for(auto z:v[p.second]){
				if(!vis[z.first]&&dis[z.first]>dis[p.second]+z.second){
					dis[z.first]=dis[p.second]+z.second;
					q.push({-dis[z.first],z.first});
				}
			}
		}
	}
}
void build(){
	for(int i=1;i<=n;i++){
		for(auto z:v[i]){
			if(dis[z.first]==dis[i]+z.second){
				Father[z.first]=i;
			}
		}
	}
}
void dfs(int x){
	if(!Father[x]){
		return;
	}
	maxn=max(D[x][Father[x]],maxn);
	minn=min(minn,D[x][Father[x]]);
	dfs(Father[x]);
}
signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		fa[i]=i;
	}
	for(int i=1,x,y,z;i<=m;i++){
		cin>>x>>y>>z;
		G.push_back({x,y,z});
	}
	sort(G.begin(),G.end());
	for(auto z:G){
		if(Find(z.x)!=Find(z.y)){
			JO(z.x,z.y);
			v[z.x].push_back({z.y,z.w});
			v[z.y].push_back({z.x,z.w});
			D[z.x][z.y]=D[z.y][z.x]=z.w;
		}
	}
	cin>>s>>t;
	Dijkstra(s);
	if(dis[t]==1e18){
		cout<<"IMPOSSIBLE";
		return 0;
	}
	build();
	dfs(t);
	int C=__gcd(maxn,minn);
	maxn/=C,minn/=C;
	cout<<maxn;
	if(minn!=1){
		cout<<'/'<<minn;
	}
	return 0;
}
2025/1/23 10:26
加载中...