ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CodeChef Starters 9 Division 3 (Rated) India Fights Corona

2021-08-18 11:32:58  阅读:248  来源: 互联网

标签:Division Rated dist idx int 短路 Fights long 多源


原题链接 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;
}

标签:Division,Rated,dist,idx,int,短路,Fights,long,多源
来源: https://www.cnblogs.com/ZhengLijie/p/15156023.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有