当前位置:网站首页>(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
2022-07-06 12:33:00 【Vigorous waist Nuo dance】
subject
1-5 317 Sub mission (100 branch )
background :
“ You walk on the plain , Suddenly I met a wall head-on , The wall is infinitely high up , Down to infinity , Left infinity , Right infinity , What is this wall ?”——《 Wandering the earth 》 The original
We took the earth to wander , In order to deal with the possible crisis in the process of wandering , The coalition found you , I hope you can help to complete 317 Sub mission : Make emergency plan .
subject :
The surface of the earth has n A stronghold , Between these strongholds m Two way road .
In these strongholds , Some are built under planetary engines , Protected by planetary engines ( Planetary engine stronghold ), Other strongholds are not protected by planetary engines ( Common stronghold , Like fuel collection sites / Scientific research base, etc ).
When there's a crisis , It's very dangerous without the protection of planetary engines , So everyone needs to go to the nearest planetary engine base for shelter , However, planetary engine sites are not necessarily safe , Plus the limited capacity of the planetary engine base , So sometimes you have to go to the second or third nearest planetary engine base .
The coalition found you , I hope you can calculate the nearest k A planetary engine base , To simplify the problem , You just need to export each site to the nearest k The sum of the shortest distances between two planetary engine sites , If there are not enough planetary engine sites that a site can reach k individual , Then output the sum of the shortest distance of all planetary engines it can reach .
Input format :
Reading data from standard input .
The first line of input contains three integers separated by spaces n, m, k , The meaning is shown in the title description , Guarantee 1 ≤ n ≤10^4, 0 ≤ m ≤ 10^4, 1 ≤ k ≤ 10^2. The strongholds are numbered as 1 To n .
The second line contains n An integer represents the type of each site in turn , Each number is 1 or 0 (1 Indicates that the corresponding site is a planetary engine site ,0 It means a common stronghold ).
Next m That's ok , Three integers per row u, v,w It means that there is a length of w Two way road connection u Base number and v No. 1 stronghold ,1 ≤ u, v ≤ n, 1 ≤ w ≤ 10^3 .
There may be double edges and self rings .
Output format :
Output to standard output .
Output n That's ok , Each line outputs an integer to represent the answer ( See Title Description ).
sample input :
7 6 2
1 0 1 0 1 1 0
1 4 1
1 2 3
2 4 4
2 3 5
2 5 7
6 7 5
sample output :
8
8
8
10
10
0
5
author
Jingrong
Company
Yanshan University
Code length limit
16 KB
The time limit
1000 ms
Memory limit
512 MB
Ideas
At first, I was mainly struggling
What is heavy edge ?
What is a self ring ?
What effect do these two goods have on this problem ?
Can't
I've only been writing questions for a month
Know very little
The double edge is the multiple assignment of the distance between two points
So when typing
May be covered
Just pay attention
Self ring is to get to your side
Figure after initialization
And then graoh[i][i] Set it to infinity
How to use dijkstra Find the nearest point k A stronghold ?
Pay attention to this k A stronghold can contain itself
Only need to use dijksra To calculate the dis After array
Yes dis Array traversal
Find the smallest , And it's a stronghold
Of course
because dijkstra You can find it every time you cycle dis The smallest one
So by the way, it's OK to take it out and store it in an array
in addition
Pay attention eight times dijkstra The independence of data is ok
That is to say
Not for public use dis and visited Array
Grade leaders use priority_queue To optimize dijkstra Of , Uh huh, study hard
Code
using namespace std;
#include<bits/stdc++.h>
int graph[10010][10010], dis[10010], visited[10010], tank[10010], a, b, c;
int n, m, k;
void dijkstra(int a) {
memset(visited, 0, sizeof visited);// Independent initialization
memset(dis, 0x3f, sizeof dis);// Independent initialization
int cnt = k;//cnt Destination , Include yourself
int result = 0;
dis[a] = 0;
vector<int>path;
for (int i = 0; i < n ; i++) {
int min = 0x3f3f3f3f, minid = -1;
for (int j = 1; j <= n; j++) {
if (dis[j] < min && !visited[j]) {
min = dis[j];
minid = j;
}
}
if (minid == -1)break;
visited[minid] = 1;
path.push_back(minid);
for (int k = 1; k <= n; k++) {
if (!visited[k] && dis[k] > dis[minid] + graph[minid][k])
dis[k] = dis[minid] + graph[minid][k];
}
}
for (int i = 0; i < path.size(); i++) {
if (!cnt)break;
if (dis[path[i]]!=0x3f3f3f3f&&tank[path[i]]) {
cnt--;
result += dis[path[i]];
}
}
cout << result << endl;
}
int main() {
memset(graph, 0x3f, sizeof graph);
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) //n A little bit ,tank Determine whether the stronghold
cin >> tank[i];
for (int i = 1; i <= m; i++) {
//m side
cin >> a >> b >> c;
graph[a][b] = graph[b][a] = c <= graph[a][b] ? c : graph[a][b];// Heavy edge
}
for (int i = 1; i <= n; i++) // Self ring
graph[i][i] = 0x3f3f3f3f;
for (int i = 1; i <= n; i++) // Come once each
dijkstra(i);
return 0;
}
边栏推荐
- [leetcode19] delete the penultimate node in the linked list
- 记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
- Minio文件下载问题——inputstream:closed
- Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
- . elf . map . list . Hex file
- Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
- dosbox第一次使用
- Talking about the startup of Oracle Database
- Basic operations of databases and tables ----- view data tables
- RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
猜你喜欢
Common properties of location
Learning notes of JS variable scope and function
Arm pc=pc+8 is the most understandable explanation
JS 函数提升和var变量的声明提升
Types de variables JS et transformations de type communes
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
ES6语法总结--上篇(基础篇)
SVN更新后不出现红色感叹号
MySQL time, time zone, auto fill 0
Kconfig Kbuild
随机推荐
[offer9] implement queues with two stacks
如何给Arduino项目添加音乐播放功能
Kconfig Kbuild
Pat 1097 duplication on a linked list (25 points)
Servlet
Kconfig Kbuild
[esp32 learning-1] construction of Arduino esp32 development environment
Detailed explanation of truncate usage
AMBA、AHB、APB、AXI的理解
2021.11.10汇编考试
[899]有序队列
程序设计大作业:教务管理系统(C语言)
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
Whistle+switchyomega configure web proxy
idea中导包方法
Esp8266 connect onenet (old mqtt mode)
ORA-02030: can only select from fixed tables/views
Common DOS commands
基于Redis的分布式锁 以及 超详细的改进思路
SVN更新后不出现红色感叹号