zoukankan      html  css  js  c++  java
  • [洛谷P4779]【模板】单源最短路径(标准版)

    题目大意:单元最短路径(卡$SPFA$)

    题解:$dijkstra$($underline{hspace{0.5em}}underline{hspace{0.5em}}gnuunderline{hspace{0.5em}}pbunderline{hspace{0.5em}}ds::priorityunderline{hspace{0.5em}}queue$优化)

    卡点:

    C++ Code:

    #include <cstdio>
    #include <ext/pb_ds/priority_queue.hpp>
    #define maxn 100010
    #define maxm 200010
    using namespace std;
    const int inf = 0x3f3f3f3f;
    int n, m, S;
    int dis[maxn];
    int head[maxn], cnt;
    struct Edge {
    	int to, nxt, w;
    } e[maxm];
    void add(int a, int b, int c) {
    	e[++cnt] = (Edge) {b, head[a], c}; head[a] = cnt;
    }
    struct cmp {
    	inline bool operator () (const int &a, const int &b) const {
    		return dis[a] > dis[b];
    	}
    };
    __gnu_pbds::priority_queue<int, cmp> q;
    __gnu_pbds::priority_queue<int, cmp>::point_iterator iter[maxn];
    void dijkstra(int S) {
    	for (int i = 1; i <= n; i++) dis[i] = inf, iter[i] = q.push(i);
    	dis[S] = 0;
    	q.modify(iter[S], S);
    	while (!q.empty()) {
    		int u = q.top(); q.pop();
    		for (int i = head[u]; i; i = e[i].nxt) {
    			int v = e[i].to;
    			if (dis[v] > dis[u] + e[i].w) {
    				dis[v] = dis[u] + e[i].w;
    				q.modify(iter[v], v);
    			}
    		}
    	}
    }
    int main() {
    	scanf("%d%d%d", &n, &m, &S);
    	for (int i = 0; i < m; i++) {
    		int a, b, c;
    		scanf("%d%d%d", &a, &b, &c);
    		add(a, b, c);
    	}
    	dijkstra(S);
    	for (int i = 1; i <= n; i++) {
    		printf("%d ", dis[i] == inf ? -1 : dis[i]);
    	}
    	puts("");
    	return 0;
    }
    

      

  • 相关阅读:
    团队项目-第二阶段冲刺1
    第十四周总结
    第十三周总结
    程序员修炼之道阅读笔记02
    第十二周总结
    程序员修炼之道阅读笔记01
    Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块
    Spring Boot 揭秘与实战 源码分析
    Spring Boot 揭秘与实战 源码分析
    Spring Boot 揭秘与实战(九) 应用监控篇
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/9460759.html
Copyright © 2011-2022 走看看