一本通1496(站外题)求助
  • 板块学术版
  • 楼主Guo1
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/1/27 20:47
  • 上次更新2024/1/27 22:34:30
查看原帖
一本通1496(站外题)求助
743879
Guo1楼主2024/1/27 20:47

估计没人会回复

#include<cstring>
#include<queue>
#include<iostream>
#define m 2001
using namespace std;
int n,p,k,A,B,L,cnt,head[m],l,r,mid,d[m];
bool b[m];
struct tel
{
	int next,to,w;
}a[m];
void add(int from,int to,int w)
{
	a[++cnt].to=to;
	a[cnt].w=w;
	a[cnt].next=head[from];
	head[from]=cnt;
}
bool spfa(int s)//最短路 
{
	queue<int>q;
	memset(d,9999999,sizeof d);
	memset(b,0,sizeof b);//一定要清零! 
	d[s]=0,b[s]=1;
	q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();b[u]=0;
		for(int i=head[u];i;i=a[i].next)
		{
			int to=a[i].to;
			if(d[to]>d[u]+a[i].w)
			{
				d[to]=d[u]+a[i].w;
				if(!b[to])
				{
					b[to]=1;
					q.push(to);
				}
			}
		}
	}
	return (d[n]<=k);
}
void binary()//二分答案 
{
	while(l<=r)
	{
		mid=(l+r)/2;
		if(spfa(mid)) r=mid-1;
		else l=mid+1;
		//cout<<l<<' '<<r<<endl;
	}
}
int main()
{
    cin>>n>>p>>k;
    r=n;
    for(int i=1;i<=p;i++) cin>>A>>B>>L,add(A,B,L),add(B,A,L);
    binary();
	if(r==n) cout<<-1<<endl;
	else cout<<r<<endl;
    return 0;
}
2024/1/27 20:47
加载中...