zoukankan      html  css  js  c++  java
  • 【最短路】POJ-2387 Til the Cows Come Home

    POJ 2387 Til the Cows Come Home

    题意:给一个无向图,求从结点n到结点1的最短路

    思路:裸的Dijkstra……邻接矩阵的话就在原来有向图的基础上多一次AddEdge操作就完事了

    const int INF = 100000+1;
    const int maxn = 1000+10;
    
    struct node {
    	LL d;
    	int u;
    	bool operator < (const node& k) const {
    		return d > k.d;
    	}
    };
    
    struct Edge {
    	int from, to;
    	LL dis;
    	Edge(int u,int v,int d):from(u),to(v),dis(d){}
    };
    
    int n, m;
    vector<Edge> Edges;
    vector<int> G[maxn];
    bool done[maxn];
    LL d[maxn];
    
    void solve(){
    	cin >> m >> n;
    	memset(done, false,sizeof(done));
    	for (int i = 1; i <= n; i++) d[i] = (i == n ? 0 : INF);
    	Edges.push_back(Edge(0, 0, 0));
    	int num = 0;
    	for (int i = 1; i <= m; i++) {
    		int u, v, d;
    		cin >> u >> v >> d;
    		Edges.push_back(Edge(u, v, d));
    		G[u].push_back(++num);
    		Edges.push_back(Edge(v, u, d));
    		G[v].push_back(++num);
    	}
    	priority_queue<node> Q;
    	Q.push(node{ 0, n });
    	while (!Q.empty()) {
    		node x = Q.top(); Q.pop();
    		int u = x.u;
    		if (done[u]) continue;
    		for (int i = 0; i < G[u].size(); i++) {
    			Edge& e = Edges[G[u][i]];
    			if (d[u] + e.dis < d[e.to]) {
    				d[e.to] = d[u] + e.dis;
    				Q.push(node{ d[e.to],e.to });
    			}
    		}
    		done[u] = true;
    	}
    	cout << d[1];
    }
    
  • 相关阅读:
    HDU 2544 最短路
    HDU 3367 Pseudoforest
    USACO 2001 OPEN
    HDU 3371 Connect the Cities
    HDU 1301 Jungle Roads
    HDU 1879 继续畅通工程
    HDU 1233 还是畅通工程
    HDU 1162 Eddy's picture
    HDU 5745 La Vie en rose
    HDU 5744 Keep On Movin
  • 原文地址:https://www.cnblogs.com/streamazure/p/12927528.html
Copyright © 2011-2022 走看看