zoukankan      html  css  js  c++  java
  • CodeChef Starters 9 Division 3 (Rated) India Fights Corona

    原题链接 India Fights Corona

    题意:

    (n)个城市,(m)条道路,其中有些城市自己有医院,所以可以在自己城市做核酸检测,那么花费就只有就医费用,而对于那些自己没有医院的城市,需要去别的城市就医,那么他们需要花的费用就是就医费 + 路费,问最小花费是多少。

    题解:

    之前只写过多源(BFS),还是记录一下多源最短路,那我就仿照着写这个多源最短路,即有医院的城市全部入堆,然后跑最短路就行,写完后发现读错题了,我以为有城市的医院只能在自己医院,实际上是可以去其他的城市的医院,那么对于处理这个这个问题,我们可以直接把自己城市的就医费用赋值给(dist[i])就行,然后跑常规最短路就好了。

    // Problem: India Fights Corona
    // Contest: CodeChef - CodeChef Starters 9 Division 3 (Rated)
    // URL: https://www.codechef.com/START9C/problems/CORONA
    // Memory Limit: 256 MB
    // Time Limit: 1000 ms
    // 
    // Powered by CP Editor (https://cpeditor.org)
    
    //多源最短路
    
    #include <bits/stdc++.h>
    
    using namespace std;
    
    typedef pair<long long, int> PLI;
    typedef long long LL;
    const int N = 2E5 + 10, M = 8E5 + 10;
    
    long long res;
    int h[N], e[M], ne[M], w[M], idx;
    LL dist[N];
    int n, m, k;
    int cost[N];
    bool st[N];
    
    void add(int a, int b, int c) {
    	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
    }
    
    void Dijkstra() {
    	for (int i = 1; i <= n; i++) dist[i] = 1e18;
    	priority_queue<PLI, vector<PLI>, greater<PLI>> heap;
    	for (int i = 1; i <= n; i++) {
    		if (cost[i]) {
    			dist[i] = cost[i];
    			heap.push({dist[i], i});
    		}
    	}
    	
    	while (heap.size()) {
    		auto t = heap.top();
    		heap.pop();
    		
    		int var = t.second;
    		
    		if (st[var]) continue;
    		st[var] = true;
    		int cst = cost[var];
    		
    		for (int i = h[var]; i != -1; i = ne[i]) {
    			int j = e[i];
    			if (dist[j] > dist[var] + w[i]) {
    				dist[j] = dist[var] + w[i];
    				heap.push({dist[j], j});
    			}
    		}
    	}
    }
    
    int main() {
    	int t; scanf("%d", &t);
    	while (t--) {
    		res = 0;
    		scanf("%d%d%d", &n, &m, &k);
    		for (int i = 1; i <= n; i++) cost[i] = 0;
    		for (int i = 1; i <= n; i++) st[i] = false;
    		for (int i = 1; i <= k; i++) {
    			int x, c;
    			scanf("%d%d", &x, &c);
    			cost[x] = c;
    		}
    		memset(h, -1, sizeof h);
    		idx = 0;
    		for (int i = 1; i <= m; i++) {
    			int a, b, c;
    			scanf("%d%d%d", &a, &b, &c);
    			add(a, b, c), add(b, a, c);
    		}
    		
    		Dijkstra();
    		
    		for (int i = 1; i <= n; i++) {
    			printf("%lld ", dist[i]);
    		}
    		puts("");
    	}
    	
        return 0;
    }
    
  • 相关阅读:
    windows 挂载windows 共享盘为本地磁盘
    模块
    message 匹配不上grok正则 也会写入到elasticsearch
    【Java】No enclosing instance of type XXX is accessible. Must qualify the allocation with an enclosing instance of type XXX(e.g. x.new A() where x is an instance of XXX).的解决办法
    【Ubuntu】常用软件及其下载地址
    【Java】内存分析
    【Java】利用二维数组进行排版
    【Java】一维数组
    【趣题】用for循环输出菱形
    【趣题】穷举法解决百钱买百鸡
  • 原文地址:https://www.cnblogs.com/ZhengLijie/p/15156023.html
Copyright © 2011-2022 走看看