POJ-1511-Invitation cards

Source: Internet
Author: User

POJ-1511-Invitation cards

Http://poj.org/problem? Id = 1511

The meaning of this question is to give some edges, find the sum of the distance between the first point and other points + the minimum value of the sum of the distance between the other points and the first point, and find the shortest distance between the two single source points, find the minimum distance between the first vertex and other points for the first time, and reverse all the directed edges for the second time, the minimum distance between the first vertex and other vertices is the minimum distance between the other vertices and the first vertex in the source image.

# Include <iostream> # include <cstdio> # include <cstring> # include <queue> using namespace STD; # define n 1000010 # define Max 0x7fffffint head [N], nextt [N], key [N], num [N], visit [N]; int t ;__ int64 dis [N]; int nodenum, edgenum; struct cam // directed edge {int St, Ed; int V;} edge [N]; void add (INT St, int ed, int V) {key [T] = V; nextt [T] = head [st]; // The Last edge num [T] = ed starting from U; head [st] = T ++; // an edge closest to the start point of St} void Init () {T = 0; memset (Head,-1, sizeof (H EAD); For (INT I = 1; I <= nodenum; I ++) dis [I] = max ;__ int64 spfa (INT start) {_ int64 Total = 0; int temp; queue <int> q; while (! Q. empty () Q. pop (); memset (visit, 0, sizeof (visit); DIS [start] = 0; visit [start] = 1; q. push (start); While (! Q. Empty () {temp = Q. Front (); q. Pop (); visit [temp] = 0; For (INT I = head [temp]; I! =-1; I = nextt [I]) {If (DIS [num [I]> dis [temp] + key [I]) {dis [num [I] = dis [temp] + key [I]; If (! Visit [num [I]) {q. push (Num [I]); visit [num [I] = 1 ;}}}for (INT I = 1; I <= nodenum; I ++) total + = dis [I]; return total;} int main () {int top, CAS ;__ int64 ans; scanf ("% d", & CAS ); while (CAS --) {Top = ans = 0; scanf ("% d", & nodenum, & edgenum); Init (); For (INT I = 0; I <edgenum; I ++) {scanf ("% d", & edge [Top]. st, & edge [Top]. ed, & edge [Top]. v); add (edge [Top]. st, edge [Top]. ed, edge [Top]. v); top ++;} ans + = spfa (1); Init (); For (INT I = 0; I <top; I ++) add (edge [I]. ed, edge [I]. st, edge [I]. v); ans + = spfa (1); printf ("% i64d \ n", ANS);} 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.