• 洛谷P2573


    Description

    (n) 个点,有各自的高度。
    (m) 条道路,有各自的长度,每条可连接两个点。
    规定只能从高点走向低点,可以回到原来的某个位置走不同的道路。
    求在行走道路尽量短的情况下到达点的数量尽量多

    Solution

    就是最小生成树加个高度判断
    思路:走一边图,搜索把从一点开始能走到的所有点放到一个新图里,然后判断高低走最小生成树
    搜索的过程是类似于 (Dijkstra) 的思想,用 (STL) 就好了
    注意手写 (cmp) 的时候把高度放在第一条件,距离放在第二条件
    其他并没有什么很难的操作

    Code

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<cstdlib>
    #include<queue>
    #define maxn 2000100
    
    using namespace std;
    
    inline int read(){
       int s=0,w=1;
       char ch=getchar();
       while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
       while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
       return s*w;
    }
    //======================================================================================
    long long n,m,h[maxn],u[maxn],v[maxn],w,cnt;
    long long head[maxn],tot1,tot2,fa[maxn],ans;
    bool vis[maxn];
    
    struct edge{
    	int to,fr,nxt,dis;
    }e1[maxn*2],e2[maxn*2];
    
    void add1(int fr,int to,int dis){
    		e1[++tot1].to=to;
    		e1[tot1].dis=dis;
    		e1[tot1].fr=fr;
    		e1[tot1].nxt=head[fr];
    		head[fr]=tot1;
    }
    
    void add2(int fr,int to,int dis){
    		e2[++tot2].to=to;
    		e2[tot2].dis=dis;
    		e2[tot2].fr=fr;
    }
    
    int esort(edge a,edge b){
    	if(h[a.to]==h[b.to]) return (a.dis<b.dis);
    	else return (h[a.to]>h[b.to]); 
    }
    //=======================================================================================
    
    int Get_Father(int x){
    	if(fa[x]==x) return x;
    	return fa[x]=Get_Father(fa[x]);
    }
    
    void hb(int x,int y){
    	x=Get_Father(x);
    	y=Get_Father(y);
    	if(x!=y) fa[y]=x;
    }
    
    bool pd(int x,int y){
    	x=Get_Father(x);
    	y=Get_Father(y);
    	if(x==y) return true;
    	return false;
    }
    
    void bfs(int x){
    	queue<int> q;
    	q.push(x);
    	vis[1]=1;
    	while(!q.empty()){
    	    int xx=q.front();
    	    q.pop();
    		for(int i=head[xx];i;i=e1[i].nxt){
    			int to=e1[i].to;
    	        add2(e1[i].fr,e1[i].to,e1[i].dis);
    			if(!vis[to]){
    				q.push(to);
    				vis[to]=1;
    			    cnt++;
    			}
    		}
    	}
    }
    
    int main(){
    	n=read();m=read();
    	for(int i=1;i<=n;i++){
    		h[i]=read();
    		fa[i]=i;
    	}
    	for(int i=1;i<=m;i++){
    		u[i]=read(),v[i]=read(),w=read();
    		if(h[u[i]]>=h[v[i]]) add1(u[i],v[i],w);
    		if(h[u[i]]<=h[v[i]]) add1(v[i],u[i],w);
    	}
    	bfs(1);
    //		add(u[i],v[i],w);
    	sort(e2+1,e2+tot2+1,esort);
    //	for(int i=1;i<=m;i++) cout<<e[i].fr<<" "<<e[i].to<<" "<<e[i].dis<<endl; 
    	for(int i=1;i<=tot2;i++){
    		if(pd(e2[i].fr,e2[i].to)) continue;
    	    hb(e2[i].fr,e2[i].to);
    	    ans+=e2[i].dis;
    	}
    	
    	printf("%lld %lld",cnt+1,ans);
    	return 0;
    }
    
  • 相关阅读:
    【读书笔记】之《把时间当做朋友》
    软件工程——五大模型
    VB中的GetUserName函数
    VB中 vbp vbw frm frx log bas 等扩展名大全
    【机房收费系统】——基本数据设定的理解
    在64位WindowsServer2012R2中安装Oracle10g第二版(10.2.0.4.0)-20160106
    Linux——vi命令详解
    使用Postman测试WebService接口
    npm和yarn的淘宝镜像添加
    yarn配置私有registry
  • 原文地址:https://www.cnblogs.com/KnightL/p/13935574.html
Copyright © 2020-2023  润新知