• 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;
    }
    

      

  • 相关阅读:
    python 开发中的常用功能
    python 栈&队列&列表的区别
    python 内置函数简介及其作用
    python 正则表达式详解
    python scrapy
    python 文件操作
    python 爬虫实例
    浅谈tcp 与udp
    php正则匹配video 中或者img的宽度和高度。
    android技术积累:开发规范
  • 原文地址:https://www.cnblogs.com/cxhscst2/p/8454662.html
Copyright © 2020-2023  润新知