zoukankan      html  css  js  c++  java
  • 洛谷 P4880 抓住czx

    水题,只需要枚举可能抓住czx的点,找出抓住的最小时间即可

    需要用lty到达czx所在位置的最小时间,一遍dijkstra即可

    注意,需要开long long 和特判t=0的情况

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<algorithm>
    #define int long long
    using namespace std;
    const int maxn=1e5+10;
    const int inf =0x3f3f3f3f;
    inline int read(){
    	int ret=0;
    	int f=1;
    	char ch=getchar();
    	while(ch<'0'||ch>'9'){
    		if(ch=='-')
    			f=-f;
    		ch=getchar();
    	}
    	while(ch<='9'&&ch>='0'){
    		ret=ret*10+(ch^'0');
    		ch=getchar();
    	}
    	return f*ret;
    }
    struct edge{
    	int nex;
    	int to;
    	int w;
    }e[maxn*10+10];
    int head[maxn*10+10];
    int cnt;
    struct node{
    	int s,d;
    	bool operator < (const node &x) const {
    		return d>x.d;
    	}
    };
    void add(int u,int v,int w){
    	cnt++;
    	e[cnt].nex=head[u];
    	e[cnt].to=v;
    	e[cnt].w=w;
    	head[u]=cnt;
    } 
    int n,m,b,o;
    int dis[maxn];
    void dist(int s){
    	for(int i=1;i<=n;i++){
    		dis[i]=inf;
    	}
    	dis[s]=0;
    	priority_queue<node>q;
    	q.push((node){s,dis[s]});
    	while(!q.empty()){
    		node f=q.top();
    		q.pop();
    		int u=f.s;
    		int d=f.d;
    		if(dis[u]!=d) {
    			continue; 
    		}
    		for(int i=head[u];i;i=e[i].nex){
    			int y=e[i].to;
    			if(dis[y]>dis[u]+e[i].w){
    				dis[y]=dis[u]+e[i].w;
    				q.push((node){y,dis[y]});
    			}
    		}
    	}	
    		return ;
    }
    int t;
    struct pos{
    	int t;
    	int p;
    }a[maxn];
    bool cmp(pos x,pos y){
    	return x.t<y.t;
    }
    signed main(){
    //	freopen("a.in","r",stdin);
    	n=read();
    	m=read();
    	b=read();
    	o=read();
    	int x,y,w;
    	for(int i=1;i<=m;i++){
    		x=read();
    		y=read();
    		w=read(); 
    		add(x,y,w);
    		add(y,x,w);
    	}
    	dist(b);
    	t=read();
    	if(!t){
    		cout<<dis[o]<<endl;
    		return 0;
    	}
    	for(int i=1;i<=t;i++){
    		a[i].t=read();
    		a[i].p=read();	
    	}
    	sort(a+1,a+1+t,cmp);
    	int ans=inf;
    	if(dis[o]<a[1].t){
    		ans=min(ans,dis[o]);
    	}
    	for(int i=1;i<=t;i++){
    		int p=a[i].p;
    		int tt=a[i].t;
    		if(dis[p]<tt){
    			ans=min(ans,tt);
    		}
    		else {
    			if(dis[p]>=tt&&(dis[p]<a[i+1].t||i==t)){
    				ans=min(ans,dis[p]);
    			}
    		}
    	}
    	cout<<ans<<endl;
    	return 0;
    }
    
  • 相关阅读:
    用C#如何创建、读取cookie
    根据拼音首字母搜索
    物理路径和相对路径 斜杠和反斜杠
    FileUpLoad导入文件类型
    JS获取FckEditor的值
    DateTime 的24小时和12小时制
    JS中Date对象getYear()方法和getFullYear()方法区别
    SQL Server 索引结构及其使用(二)
    带你学习JQuery:事件冒泡和阻止默认行为
    DropDownList 发现
  • 原文地址:https://www.cnblogs.com/rpup/p/14032405.html
Copyright © 2011-2022 走看看