< graph algorithm pta> 1003 Emergency (25 points)

After writing this question today, I feel that the connection between theory and practice is not enough. I can think and code; From easy to difficult, start slowly ~

In fact, the code refers to the algorithm notes

This is an application of dijkstra algorithm; The point weight is added, and the edge weight should be an old friend, but the problem is that the topic requires the number of shortest paths and the maximum point weight. How to maintain these two values while the algorithm is running is something I haven't covered before. So I decided to learn this problem by example and read the analysis in the book.

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int inf=1000000;
int main(void){
int visited={0};//City access array
int rescue={0};//Number of rescue teams
int dist={0};//Shortest distance to each point
int edge={0};
int n,m,c1,c2;
scanf("%d %d %d %d",&n,&m,&c1,&c2);
for(int i=0;i<n;i++){
scanf("%d",&rescue[i]);
}
fill(edge,edge+501*501,inf);//For initialization
fill(dist,dist+501,inf);
dist[c1]=0;

for(int i=0;i<m;i++){//Structural map
int t1,t2,value;
scanf("%d %d %d",&t1,&t2,&value);
edge[t1][t2]=value;
edge[t2][t1]=value;
}

int w={0};
int path_num={0};
w[c1]=rescue[c1];
path_num[c1]=1;
for(int i=0;i<n;i++){
int minn=inf,minn_index=-1;
for(int j=0;j<n;j++){
if(minn>dist[j]&&visited[j]==0){
minn=dist[j];
minn_index=j;
}
}
if(minn_index==-1) break;
visited[minn_index]=1;
for(int j=0;j<n;j++){
if(visited[j]==0&&edge[minn_index][j]!=inf){
if(dist[j]>dist[minn_index]+edge[minn_index][j]){//If the current path is fuller than one circle, modify it
dist[j]=dist[minn_index]+edge[minn_index][j];
w[j]=w[minn_index]+rescue[j];
path_num[j]=path_num[minn_index]; //cover path_num[j]
}
else if(dist[j]==dist[minn_index]+edge[minn_index][j]){
path_num[j]=path_num[minn_index]+path_num[j];//The number of shortest paths has nothing to do with the point weight and is written outside
if(w[j]<w[minn_index]+rescue[j]){ //If there are more rescue teams
w[j]=w[minn_index]+rescue[j];//Inherit more rescue teams
}
}
}
}
}

printf("%d %d\n",path_num[c2],w[c2]);
return 0;
}

Because I haven't touched the STL of c + + for a long time, I forgot many easy-to-use data structures in the past, so I can't think of it

Summarize what you learned today

Q1: difference between memset and fill

memset

1. Header file: #include < string h>
2. It is generally used to fill char arrays
3. If memset is used to fill the int array, only 0, - 1 and INF (0x3f3f3f3f) can be assigned. Otherwise, an error will occur and - 1 will be assigned directly

fill

Header file: #include < algorithm >

Fill (start position, end, value) can be assigned arbitrarily

fill(edge,edge+501*501,inf); Assign a value to a two-dimensional array

Q2: I don't remember how to use vector So I read the document

To summarize the vector feature:

1.vector is an array that can be changed in size. The efficiency of accessing an element in the array is the same. However, changes in the size of the vector are handled automatically by the container

2. When a new element is inserted, the vector may need to request a new array and move all the elements, but the vector does not do this every time it is inserted.

3. In fact, vector keeps his hands on the above problems. Although it is said that it is growing dynamically, he will still apply for more space to deal with unexpected needs

4. The official suggestion is that the insertion needs to be carried out at the interval of exponential growth in order to share the time complexity? I don't quite understand

But when I see someone else's blog on the Internet, I compare vector insertion with list insertion; The former has better performance. The reason should be that the vector has a first-hand feature, which makes the insertion efficiency at the tail very high. However, each insertion of the list needs to apply for a new node, so the insertion efficiency of the vector is still very strong

However, the official documents say that if you insert or delete at a specific location, the vector is better than deque,list and forward_list difference

5. Compared with arrays, vector consumes more memory because of its effective storage management and dynamic growth

Create a new vector: vector < int > num [size]. < > It can also be a structure, which can be used for drawing collection and storage

Insert a new element at the tail: push_back()

empyt() checks whether the vector is empty

capacity() returns the actual allocated size of the vector

shrink_ to_ Fit () is only supported by C + + 11, which is to make capacity and size the same

Let's look at the official documents. You can only remember language related things if you use them all the time, otherwise you will forget them quickly, but since you have learned it, it's faster to pick it up again than to start from scratch! Added by Im Jake on Sun, 09 Jan 2022 05:40:57 +0200
{"ft_lang_label":"__label__en","ft_lang_prob":0.8459341,"math_prob":0.93796533,"size":4888,"snap":"2022-05-2022-21","text_gpt3_token_len":1262,"char_repetition_ratio":0.11875512,"word_repetition_ratio":0.0,"special_character_ratio":0.2757774,"punctuation_ratio":0.12772277,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9815475,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-01-17T22:38:24Z\",\"WARC-Record-ID\":\"<urn:uuid:7da3b147-68fa-4bd8-8ec8-66d63fa6253d>\",\"Content-Length\":\"17908\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bdefe32c-02ae-4136-a9b4-2070ab218fc4>\",\"WARC-Concurrent-To\":\"<urn:uuid:f0f76251-783c-4c36-81b3-cda8ec9864c6>\",\"WARC-IP-Address\":\"185.218.125.0\",\"WARC-Target-URI\":\"https://programming.vip/docs/graph-algorithm-pta-1003-emergency-25-points.html\",\"WARC-Payload-Digest\":\"sha1:JVEP6UYXDLJEIKEYLB2ZY46K4NY3HM6D\",\"WARC-Block-Digest\":\"sha1:H6BMIRWEHHLSIUYEYWRRVWVVLFSSN6A4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-05/CC-MAIN-2022-05_segments_1642320300624.10_warc_CC-MAIN-20220117212242-20220118002242-00025.warc.gz\"}"}