HDU1535 Invitation Cards Shortest circuit

Source: Internet
Author: User

Portal: "HDU" 1535 invitation Cards


Topic Analysis: The topic is really difficult to read ... In fact, the problem is to ask for the sum of the shortest distance from the point numbered 1 to all other points, plus all other points to the shortest distance of the points numbered 1. As long as the original image and the reverse diagram to run the shortest time is good.


The code is as follows:


#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define REP (I, A, b) for (int i = (a); I < (b); + + i) #define for (I, A, b) for (int i = (a); I < = (b); + + i) #define REV (I, A, b) for (int i = (a); I >= (b);---i) #define Travel (E, H, u) for (edge* e = h [u]; E
E = e = next) #define CLR (A, X) memset (A, x, sizeof a) const int MAXN = 1000005;
const int MAXH = 2000005;
const int maxe = 2000005;

const int INF = 0X3F3F3F3F;
	struct Edge {int V, C;
Edge* Next;

} ;
	
	struct Heap {int V, IDX; Heap () {} heap (int v, int idx): V (v), IDX (IDX) {} BOOL operator < (const heap& a) const {RE
	Turn v < A.V;

}
} ;
	struct Priority_queue {Heap heap[maxh];
	
	int point;
	Priority_queue (): Point (1) {} void Clear () {point = 1;
	} bool Empty () {return point = = 1;
		} void maintain (int o) {int x = O; while (o &GT
			1 && heap[o] < heap[o >> 1]) {swap (Heap[o], heap[o >> 1]);
		o >>= 1;
		} o = x; int p = o, L = o << 1, r = O << 1 |
		1;
			while (O < point) {if (L < point && Heap[l] < heap[p]) P = l;
			if (R < point && Heap[r] < heap[p]) P = r;
			if (p = = O) break;
			Swap (Heap[o], heap[p]); o = p, l = o << 1, r = O << 1 |
		1;
		}} void push (int v, int idx) {Heap[point] = heap (v, IDX);
	Maintain (point + +);
		} void Pop () {heap[1] = heap[--Point];
	Maintain (1);
	} int Front () {return heap[1].idx;
	} Heap Top () {return heap[1];

}
} ;
	struct Shortest_path_algorithm {priority_queue q;
	Edge E[maxe], *H[MAXN], *cur;
	Edge Re[maxe], *RH[MAXN], *rcur;
	int D[MAXN];
	BOOL VIS[MAXN];
	
	int Q[MAXN], head, tail;
		void init () {cur = E;
		Rcur = RE;
		CLR (H, 0);
	CLR (RH, 0); } void Addedge (intu, int v, int c) {cur v = v;
		Cur c = c;
		Cur next = h[u];
		H[u] = cur + +;
		Rcur v = u;
		Rcur C = C;
		Rcur next = Rh[v];
	RH[V] = rcur + +;
		} void Dijkstra (int s, edge* h[]) {q.clear ();
		CLR (d, INF);
		CLR (Vis, 0);
		D[s] = 0;
		Q.push (D[s], s);
			while (!q.empty ()) {int u = q.front ();
			Q.pop ();
			if (Vis[u]) continue;
			Vis[u] = 1;
				Travel (E, H, u) {int v = e, V, C = e-C;
					if (D[v] > D[u] + c) {D[v] = D[u] + C;
				Q.push (D[v], V);
		}}}} void Spfa (int s, edge* h[]) {head = tail = 0;
		CLR (d, INF);
		CLR (Vis, 0);
		D[s] = 0;
		Q[tail + +] = s;
			while (head! = tail) {int u = q[head + +];
			if (head = = MAXN) head = 0;
			Vis[u] = 0;
				Travel (E, H, u) {int v = e, V, C = e-C;
					if (D[v] > D[u] + c) {D[v] = D[u] + C;
if (!vis[v]) {vis[v] = 1;						if (D[v] < D[q[head]]) {if (head = = 0) head = MAXN;
						q[--Head] = v;
							} else {Q[tail + +] = V;
						if (tail = = maxn) tail = 0;

}}}}}} G;


int n, m;
	void scanf (int& x, char c = 0) {while ((c = GetChar ()) < ' 0 ' | | c > ' 9 ');
	x = C-' 0 ';
while ((c = GetChar ()) >= ' 0 ' && C <= ' 9 ') x = x * + C-' 0 ';
	} void Solve () {int u, V, c;
	int ans = 0;
	G.init ();
	SCANF (n), scanf (m);
		while (M--) {scanf (U), scanf (v), scanf (c);
	G.addedge (U, V, c);
	} g.dijkstra (1, G.H);
	For (i, 1, n) ans + = g.d[i];
	G.dijkstra (1, G.RH);
	For (i, 1, n) ans + = g.d[i];
printf ("%d\n", ans);
	} int main () {int T;
	scanf ("%d", &t);
	while (T--) solve ();
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.