PKU 1511 invitation cards

Source: Internet
Author: User

This topic is limited to 8000 ms, which is not very easy at first glance, but the meaning of the question is very simple.

Calculates the minimum value of the sum of the source and other vertices plus the sum of the other vertices and The Source Vertex.
Create two adjacent tables for the front and back, and perform the two short circuits for the front and back, spfa
Reverse Order: another identical table is saved in reverse order during initialization.
Namely: 1 ---> 2 13
After reverse: 2 ---> 1 13
The distance between each point and the source point is required, that is, the distance from the source point to each point and
Therefore, after processing, only the distance from two source points to each point and the sum are required.

# Include <iostream> # include <string> # include <stdlib. h> using namespace STD; const int max = 1000001; const int INF = int_max; int n, m, n, num; typedef struct vol {int W, // current position V, // value valuenext; // location of the next node} VOLL; VOLL peo [Max * 2]; bool vis [Max]; int start1 [Max], start2 [Max]; int stack [Max]; int dis [Max]; // dis [I] from 1 to the current shortest _ int64 spfa (INT startt []) {int I, top = 0, temp; temp = 1; for (I = 0; I <n + 1; I ++) {dis [I] = inf;} memset (VIS, false, sizeof (VIS); DIS [temp] = 0; stack [++ top] = temp; vis [temp] = true; while (top) {temp = stack [top --]; vis [temp] = false; for (I = startt [temp]; I! =-1; I = peo [I]. next) {If (peo [I]. V + dis [temp] <dis [peo [I]. w]) {dis [peo [I]. w] = peo [I]. V + dis [temp]; If (! Vis [peo [I]. w]) {vis [peo [I]. w] = true; stack [++ top] = peo [I]. W ;}}__ int64 sum = 0; for (I = 1; I <= N; I ++) {sum + = dis [I];} return sum;} void Init () {scanf ("% d", & N, & M); int I, X, Y, V; num = 0; memset (start1,-1, sizeof (start1); memset (start2,-1, sizeof (start2); for (I = 0; I <m; I ++) {scanf ("% d", & X, & Y, & V); peo [num]. W = y; peo [num]. V = V; peo [num]. next = start1 [X]; // Save the start and end positions of the adjacent table string. The reverse implementation is awesome !!! Start1 [x] = num ++; // reverse peo [num]. W = x; peo [num]. V = V; peo [num]. next = start2 [y]; start2 [y] = num ++ ;}} int main () {scanf ("% d", & N); While (n --) {Init (); printf ("% i64d \ n", spfa (start1) + spfa (start2);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.