求助!
  • 板块学术版
  • 楼主ainivolAGEM
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/7/20 10:30
  • 上次更新2023/11/3 08:43:08
查看原帖
求助!
551428
ainivolAGEM楼主2023/7/20 10:30

这有一道来自GFOJ的题目,我把题面放在了这里。

但是这道题我怎么改都输出的是 −1-1 是为什么呢?求助!

#include<bits/stdc++.h>
using namespace std;
typedef long ll;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int N=1004;
const int P=2004;
const int R=10000000;
ll n,p,k,t[N];
ll l=1,r=R;
ll a,b,l1,tot;
ll dis[N],head[P];
bool vis[N];
struct edge{
	ll next,to,dis;
}e[P];
struct node{
	ll id,dis;
	bool operator<(const node &tmp)const{
		return dis>tmp.dis;
	}
};
priority_queue<node> q;
void add_edge(ll u,ll v,ll w){
	e[++tot].dis=w;
	e[tot].next=head[u];
	head[u]=tot;
	e[tot].to=v;
}
void dijkstra(){
	for(int i=1;i<=n;i++){
		dis[i]=inf;
		vis[i]=false;
	}
	dis[n]=0;
	q.push({n,0});
	while(!q.empty()){
		node cur=q.top();
		q.pop();
		ll u=cur.id;
		if(vis[u]){
			continue;
		}
		vis[u]=true;
		for(int i=head[u];i;i=e[i].next){
			ll v=e[i].to;
			if(dis[v]>dis[u]+e[i].dis){
				dis[v]=dis[u]+e[i].dis;
				q.push((node){v,dis[v]});
			}
		}
	}
}
bool check(ll r){
	for(int i=1;i<=p;i++){
		if(e[i].dis<=r){
			t[i]=e[i].dis;
			e[i].dis=0;
		}else{
			t[i]=e[i].dis;
			e[i].dis=1;
		} 
	}
	dijkstra();
	for(int i=1;i<=p;i++){
		e[i].dis=t[i];
	}
	if(dis[1]<=k){
		return true;
	}else{
		return false;
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>p>>k;
	for(int i=1;i<=p;i++){
		cin>>a>>b>>l1;
    	add_edge(a,b,l1);
    	add_edge(b,a,l1);
	}
	bool flag=false;
	while(r-l>=1){
		ll mid=(l+r)>>1;
		if(check(mid)){
			l=mid;
			flag=true;
		}else{
			r=mid-1;
		}
	}
	if(!flag){
		cout<<"-1";
		exit(0);
	}
	cout<<r;
}

有哪位大佬能指出一下问题吗?谢谢!!!

2023/7/20 10:30
加载中...