10pts求助
查看原帖
10pts求助
557270
2021sunzishan楼主2023/7/19 14:03

code:

#include <bits/stdc++.h>
using namespace std;
#define int long long
bool vis[100005];
int head[1000005],cnt,dis[100005];
int n,m,t,s,e;
struct node {
	int next,to,w;
} edge[1000005];
struct node1 {
	int t,x;
} a[1000005];
bool  cmp(node1 a,node1 b) {
	return a.t<b.t;
}
void addedge(int a,int b,int z) {
	edge[++cnt].next=head[a];
	head[a]=cnt;
	edge[cnt].to=b;
	edge[cnt].w=z;
}
struct pnode {
	int dis,k;
	bool operator>(const pnode &x)const {
		return dis>x.dis;
	}
};
priority_queue<pnode,vector<pnode>,greater<pnode> >q;
inline int read() {
	int a=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9') {
		if  (c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9') {
		a=a*10+(c-'0');
		c=getchar();
	}
	return f*a;
}
main() {
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	n=read(),m=read(),s=read(),e=read();
	for(int i=1; i<=m; i++) {
		int u=read(),v=read(),z=read();
		addedge(u,v,z);
	}
	for(int i=1; i<=n; i++)
		dis[i]=INT_MAX;
	dis[s]=0;
	pnode k;
	k.dis=0,k.k=s;
	q.push(k);
	while(!q.empty()) {
		pnode now=q.top();
		q.pop();
		int u=now.k;
		if(vis[u])continue;
		vis[u]=1;
		for(int i=head[u]; i; i=edge[i].next) {
			int v=edge[i].to;
			if(!vis[v]&&dis[v]>dis[u]+edge[i].w) {
				dis[v]=dis[u]+edge[i].w;
				pnode k;
				k.dis=dis[v],k.k=v;
				q.push(k);
			}
		}
	}
	t=read();
	for(int i=1; i<=t; i++) {
		a[i].t=read();
		a[i].x=read();
	}
	sort(a+1,a+1+t,cmp);
	a[++t].t=0,a[t].x=e;
	for(int i=1; i<=t; i++) 
		if(dis[a[i].x]<a[i+1].t) {
			printf("%lld\n",max(dis[a[i].x],a[i].t));	
			return 0;
		}
	printf("%lld\n",max(dis[a[t].x],a[t].t));
	return 0;
}

2023/7/19 14:03
加载中...