zoukankan      html  css  js  c++  java
  • 洛谷 P1629 邮递员送信

    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
    typedef long long ll;
    
    const int N = 1e3 + 10;
    
    int n, m, dis[N];
    bool vis[N];
    vector<pair<int, int> > G[N];
    vector<pair<int, int> > Gf[N];
    
    void spfa() {
    	memset(dis, 0x3f, sizeof dis);
    	dis[1] = 0;
    	vis[1] = true;
    	queue<int> q;
    	q.push(1);
    	while (!q.empty()) {
    		int u = q.front();
    		q.pop();
    		vis[u] = 0;
    		for (int i = 0; i < G[u].size(); ++i) {
    			int v = G[u][i].first;
    			int w = G[u][i].second;
    			if (dis[u] + w < dis[v]) {
    				dis[v] = dis[u] + w;
    				if (vis[v] == 0) {
    					vis[v] = 1;
    					q.push(v);
    				}
    			}
    		}
    	}
    }
    void spfa2() {
    	memset(dis, 0x3f, sizeof dis);
    	dis[1] = 0;
    	vis[1] = true;
    	queue<int> q;
    	q.push(1);
    	while (!q.empty()) {
    		int u = q.front();
    		q.pop();
    		vis[u] = 0;
    		for (int i = 0; i < Gf[u].size(); ++i) {
    			int v = Gf[u][i].first;
    			int w = Gf[u][i].second;
    			if (dis[u] + w < dis[v]) {
    				dis[v] = dis[u] + w;
    				if (vis[v] == 0) {
    					vis[v] = 1;
    					q.push(v);
    				}
    			}
    		}
    	}
    }
    
    
    int main() {
    	cin >> n >> m;
    	while (m--) {
    		int u, v, w;
    		cin >> u >> v >> w;
    		G[u].push_back(make_pair(v, w));
    		Gf[v].push_back(make_pair(u, w));
    	}
    	spfa();
    	int res = 0;
    	for (int i = 1; i <= n; ++i) res += dis[i];
    	spfa2();
    	for (int i = 1; i <= n; ++i) res += dis[i];
    	cout << res;
        return 0;
    }
    
    
  • 相关阅读:
    java web 自定义的权限框架
    java web 中 读取windows图标并显示
    apache httpclient
    Gson json解析工具
    org.apache.commons
    ehcache 缓存管理工具
    springmvc集成Freemarke配置的几点
    SaltStack与ZeroMQ(二)
    Python中的__init__和__new__介绍
    TCP 介绍
  • 原文地址:https://www.cnblogs.com/all-taker/p/12910283.html
Copyright © 2011-2022 走看看