zoukankan      html  css  js  c++  java
  • 7-14 喊山

    思路

    这题就是一道裸的dijkstra,但是一定要注意是能传到的其他点的最大值,如果只能传到自己,这时候应该返回一个0。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    
    const int maxn=1e4+10;
    const int INF=0x3f3f3f3f;
    vector<int> g[maxn];
    int d[maxn],vis[maxn];
    
    struct Node {
    	int id;
    	int dis;
    	Node(int i,int d) :id(i),dis(d) {}
    	bool operator < (const Node& a) const {
    		return dis>a.dis;
    	}
    };
    
    int dijkstra(int s) {
    	memset(vis,0,sizeof(vis));
    	memset(d,INF,sizeof(d));
    	d[s]=0;
    	priority_queue<Node> pq;
    	pq.push(Node(s,0));
    	int res=1;
    	int id=INF;
    	while (!pq.empty()) {
    		Node f=pq.top();
    		pq.pop();
    		if (vis[f.id]) {
    			continue;
    		}
    		if (f.dis>res) {
    			res=f.dis;
    			id=f.id;
    		}
    		else if (f.dis==res&&f.id<id) {
    			id=f.id;
    		}
    		vis[f.id]=1;
    		for (auto next:g[f.id]) {
    			if (!vis[next]&&f.dis+1<d[next]) {
    				d[next]=f.dis+1;
    				pq.push(Node(next,d[next]));
    			}
    		}
    	}
    	return id==INF?0:id;
    }
    
    int main()
    {
    	int n,m,k;
    	int v1,v2;
    	scanf("%d%d%d",&n,&m,&k);
    	while (m--) {
    		scanf("%d%d",&v1,&v2);
    		g[v1].push_back(v2);
    		g[v2].push_back(v1);
    	}
    	while (k--) {
    		scanf("%d",&v1);
    		printf("%d
    ",dijkstra(v1));
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    POJ 3276 Face The Right Way
    POJ 3061 Subsequence
    HDU 2104 hide handkerchief
    GCJ Crazy Rows
    HDU 1242 Rescue
    激光炸弹:二维前缀和
    I Hate It:线段树:单点修改+区间查询
    承压计算:模拟+double
    等差素数列:线性筛+枚举
    Period :KMP
  • 原文地址:https://www.cnblogs.com/xyqxyq/p/12324138.html
Copyright © 2011-2022 走看看