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]>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]>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<<SUM<<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