zoukankan      html  css  js  c++  java
  • Codeforces 938D Buy a Ticket (转化建图 + 最短路)

    题目链接  Buy a Ticket

    题意   给定一个无向图。对于每个$i$ $in$ $[1, n]$, 求$minleft{2d(i,j) + a_{j} ight}$

    建立超级源点$n+1$, 对于每一条无向边$(x, y, z)$,$x$向$y$连一条长度为$2z$的边,反之亦然。

    对于每个$a_{i}$, 从$i$到$n+1$连一条长度为$a_{i}$的边,反之亦然。

    然后跑一边最短路即可。

    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define rep(i, a, b)	for (int i(a); i <= (b); ++i)
    #define dec(i, a, b)	for (int i(a); i >= (b); --i)
    #define MP		make_pair
    #define fi		first
    #define se		second
    
    
    typedef long long LL;
    
    const int N = 2e5 + 10;
    
    int n, m;
    LL dis[N];
    
    struct node{
    	int u;
    	LL w;
    	friend bool operator < (const node &a, const node &b){
    		return a.w > b.w;
    	}
    };
    
    vector <node> v[N];
    
    void dij(int s, LL dis[], vector <node> v[]){
    	priority_queue <node> q;
    	static bool vis[N];
    	rep(i, 1, n) dis[i] = 1e18, vis[i] = false;
    	q.push({s, 0});
    	dis[s] = 0;
    	while (!q.empty()){
    		int u = q.top().u; q.pop();
    		if (vis[u]) continue;
    		vis[u] = 1;
    		for (auto edge : v[u]) if (dis[u] + edge.w < dis[edge.u]){
    			dis[edge.u] = dis[u] + edge.w;
    			q.push({edge.u, dis[edge.u]});
    		}
    	}
    }
    
    
    int main(){
    
    	scanf("%d%d", &n, &m);
    	rep(i, 1, m){
    		int x, y;
    		LL z;
    		scanf("%d%d%lld", &x, &y, &z);
    		v[x].push_back({y, z * 2});
    		v[y].push_back({x, z * 2});
    	}
    
    	rep(i, 1, n){
    		LL x;
    		scanf("%lld", &x);
    		v[n + 1].push_back({i, x});
    		v[i].push_back({n + 1, x});
    	}
    
    	dij(n + 1, dis, v);
    	rep(i, 1, n) printf("%lld ", dis[i]);
    	return 0;
    }
    

      

  • 相关阅读:
    PAT 1088. Rational Arithmetic
    PAT 1087. All Roads Lead to Rome
    PAT 1086. Tree Traversals Again
    PAT 1085. Perfect Sequence
    PAT 1084. Broken Keyboard
    PAT 1083. List Grades
    PAT 1082. Read Number in Chinese
    求最大公因数
    [转载]Latex文件转成pdf后的字体嵌入问题的解决
    [转载]Matlab有用的小工具小技巧
  • 原文地址:https://www.cnblogs.com/cxhscst2/p/8454662.html
Copyright © 2011-2022 走看看