HDU 1535 Invitation Cards

Source: Internet
Author: User

This data volume is amazing, but the difficulty is not very difficult to build a map after the reverse, run 2 times SPFA algorithm is good, super memory, please submit with C + +

#include <iostream> #include <vector> #include <queue> #include <cstring> #define INF 1<< 30#define maxn 1000010using namespace std;struct stu{int next,l;}; Vector<stu>mapp1[maxn];vector<stu>mapp2[maxn];int n;int d[maxn];void Init () {for (int i=1;i<=n;i++) Mapp1[i].clear (), Mapp2[i].clear ();} void input () {int m;cin>>n>>m;for (int i=0;i<m;i++) {int x,y,z;cin>>x>>y>>z;stu root; Root.next=y;root.l=z;mapp1[x].push_back (root); Root.next=x;mapp2[y].push_back (root);}} void BFs (int flag) {Fill (d,d+n+1,inf);d [1]=0;queue<int>q;q.push (1); int x,y;while (Q.size ()) {X=q.front (); Q.pop (); if (flag==1) {for (int i=0;i<mapp1[x].size (); i++) {stu Root=mapp1[x][i];y=root.next;if (d[y]==inf| | D[Y]&GT;D[X]+ROOT.L) {D[y]=d[x]+root.l;q.push (y);}}} else {for (int i=0;i<mapp2[x].size (); i++) {stu Root=mapp2[x][i];y=root.next;if (d[y]==inf| | D[Y]&GT;D[X]+ROOT.L) {D[y]=d[x]+root.l;q.push (y);}}}}} void Solve () {int sum=0;bfs (1); for (int i=1;i<=n;i++) sum+=d[i];//COUT&LT;&LT;SUM&LT;&LT;ENDL;BFS (2); for (int i=1;i<=n;i++) Sum+=d[i];cout<<sum<<endl;} int main () {Cin.sync_with_stdio (false); int T;cin>>t;while (t--) {init (); input (); Solve ();}  return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1535 Invitation Cards

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.