POJ 1511 Invitation CARDS__SPFA Shortest Path

Source: Internet
Author: User

The main effect of the topic:

Not many people would go to the theatre in the TV age, and the old comedy artist at the Malidinesia Theater cared about it, and he wanted to publicize his ancient comic art to the public. The theater has printed many invitations (with a lot of necessary information and a theater program), many students are hired to distribute these invitations (flyers), which are assigned to designated bus stations to distribute the leaflets to commuters, and, of course, the students are trained before handing out leaflets, Teach them how to influence people rather than force them.

The city's traffic system is very special, each bus line is one-way (that is, the map), each line is connected to two bus stations, not half an hour there is a bus carrying passengers leave predicate, when the end of the station will return (back to the starting point), the cost between the stations recorded in a table, the cost can be paid on the spot, There is a CCS station on the line that requires routine security checks, including body scans.

All ACM members depart from CCS every morning to distribute leaflets in advance to designated stations, and the number of members and stations is equal, at the end of each day, each member returns to the CCS station to finish the day's work, and now needs to calculate the minimum traffic cost of the day.

There are several examples (given in the number of examples), P station Q line (each line will specify the starting station, target station and cost), of which 1≤p, q≤1,000,000, the cost of each line C < 1,000,000,000.

Topic link

SPFA (Shortest path fast algorithm, shortest path quick algorithm):

Comment Code:

* * Problem ID:POJ 1511 Invitation Cards * Author:lirx.t.una * Language: GCC * Run time:1688 MS * Run memory:28784 KB */#pragma gcc optimize ( "O2") #include <string.h> #include <stdio.h>//definition null pointer//Because the adjacency table is implemented using an array method//Thus the pointer type becomes an integral type #define NIL 0 #defin E TRUE 1 #define FALSE 0//maximum number of stops and bus lines//buses and buses total//All 1,000,000 #define MAXN 1000001//in
Finity//That is the limit of the total cost//Here is the maximum value of long long #define INF 0x1000000000000000 typedef long INT64;

typedef char BOOL; typedef struct {int u;//start station int v;//target station int c;//cost, distance cost} arc;//arc, that is to have to edge node//The following three arrays form the adjacency table data structure arc arc[maxn];//has
To the edge//side of the header, where head[u] means that//the table header of the linked list represented by the direct route from the U point//here Head[u] points to a linked table header int HEAD[MAXN with a pointing edge starting with u;
A pointer//next[i] on the next edge of an edge in a side list indicates the pointer int next[maxn of the next edge of the edge I;
  /* Total adjacency table Logical structure is such: U arc 1-> 3 (1, 4, Ten)->7 (1, 9,) 2-> 5 (2, 8, 9)->14 (2, 12, 100) ...... * *//in[i] Indicates whether the station I is in the stack BOOL IN[MAXN]; int E;//number of Edge, representing the ordinal of an edge INT64 dp[maxn];//dynamic programming holds the minimum cost of 1 to each point//dp[i] represents the current minimum cost of 1 to I point int STK[MAXN];//SPFA
	stack int top;//stack top pointer void addarc (int u, int v, int c) {//Add edge to adjacency table arc[e].u = u;
	ARC[E].V = v;

	ARC[E].C = C;
	Next[e] = Head[u];
Head[u] = e++;
	} void init (int n, int m) {//The adjacency table is initialized int u;
	int V;

	int C;
	memset (head + 1, NIL, n * sizeof (int));

	memset (next + 1, NIL, M * sizeof (int));
	e = 1;
		while (m--) {scanf ("%d%d%d", &u, &v, &c);
	AddArc (U, V, c);
	} void Uinit (int n, int m) {//uninit, reverse graph int i;

	ARC atmp;
	memset (head + 1, NIL, n * sizeof (int));

	memset (next + 1, NIL, M * sizeof (int));
	e = 1;
		for (i = 1; I <= m i++) {atmp = Arc[i];
	AddArc (ATMP.V, atmp.u, atmp.c);

	} BOOL Relax (int u, int v, int c) {//SPFA slack INT64 i64tmp;

	I64tmp = Dp[u] + (INT64) C;
		if (I64tmp < dp[v]) {dp[v] = i64tmp;
	return TRUE;
return FALSE; } void
SPFA (int n) {//1 to point shortest path (that is, minimum cost) int i, j;//count variable int u, v;//start, end//initialization dp[1] = 0;//oneself to oneself not money for (i = 2; I <= N i++) Dp[i] = inf;//costs are set to maximum memset (in + 1, FALSE, n * sizeof (BOOL)) before dynamic planning;//All still in the station in[1] = true;//First inbound top = 1; Top=0 is empty stk[1] = 1;//1 node inbound/outbound below is SPFA dynamic planning//process similar breadth First search//condition is to satisfy the relaxation condition (i.e. resilient) nodes (and also sinks) into the stack for the next expansion//because only flexible while (t
		
			OP) {u = stk[top--];//stacks top stack and record stack in[u] = false;//record stack for (j = head[u]; j; j = Next[j]) {//Extend the Stack element
			v = arc[j].v; Note: This is a side effect of relaxation, that is, to judge Slack while also dynamic planning of the IF (Relax (U, V, arc[j].c) &&!in[v]) {//if flexible and not on the stack//as a good molecule into
				Stack wait next extension stk[++top] = v;
			IN[V] = TRUE;
	}} INT64 sum (int n) {//return 1 to each point of DP and INT64 s;

	int i;

	for (s = 0, i = 1; I <= N; i++) s + = Dp[i];
return s;
	int main () {int t;//measure int n, m;//site and line number INT64 tot;//total fee of a day, total cost scanf ("%d", &t);
		while (t--) {scanf ("%d%d", &n, &m); Init (n, m);//The beginning of the mapStart SPFA (n),//Find the minimum cost of 1 to each point tot = SUM (n);//Add one time Uinit (n, m);//reverse map SPFA (n);
	Tot + = SUM (n);//Add again, reverse cost sum printf ("%lld\n", tot);
return 0; }

No comment code:

#pragma GCC optimize ("O2") #include <string.h> #include <stdio.h> #define NIL 0 #define TRUE 1 #define FA
LSE 0 #define MAXN 1000001 #define INF 0x1000000000000000 typedef long INT64;

typedef char BOOL;
	typedef struct {int u;
	int V;
int C;

} ARC;
ARC ARC[MAXN];
int HEAD[MAXN];
int NEXT[MAXN];

BOOL IN[MAXN];

int e;

INT64 DP[MAXN];
int STK[MAXN];

int top;
	void AddArc (int u, int v, int c) {arc[e].u = u;
	ARC[E].V = v;

	ARC[E].C = C;
	Next[e] = Head[u];
Head[u] = e++;
	} void init (int n, int m) {int u;
	int V;

	int C;
	memset (head + 1, NIL, n * sizeof (int));

	memset (next + 1, NIL, M * sizeof (int));
	e = 1;
		while (m--) {scanf ("%d%d%d", &u, &v, &c);
	AddArc (U, V, c);
	} void Uinit (int n, int m) {int i;

	ARC atmp;
	memset (head + 1, NIL, n * sizeof (int));

	memset (next + 1, NIL, M * sizeof (int));
	e = 1;
		for (i = 1; I <= m i++) {atmp = Arc[i];
	AddArc (ATMP.V, atmp.u, atmp.c);} BOOL Relax (int u, int v, int c) {INT64 i64tmp;

	I64tmp = Dp[u] + (INT64) C;
		if (I64tmp < dp[v]) {dp[v] = i64tmp;
	return TRUE;
return FALSE;
	} void Spfa (int n) {int i, J;

	int u, v;
	DP[1] = 0;
	for (i = 2; I <= n; i++) dp[i] = INF;
	memset (in + 1, FALSE, n * sizeof (BOOL));
	IN[1] = TRUE;
	top = 1;

	STK[1] = 1;
		while (top) {u = stk[top--];

		In[u] = FALSE;
			for (j = head[u]; j; j = Next[j]) {v = arc[j].v;
				if (Relax (U, V, arc[j].c) &&!in[v]) {Stk[++top] = V;
			IN[V] = TRUE;
	INT64 sum (int n) {INT64 s;}}}}

	int i;

	for (s = 0, i = 1; I <= N; i++) s + = Dp[i];
return s;
	int main () {int t;
	int n, m;

	INT64 tot;
	scanf ("%d", &t);
		while (t--) {scanf ("%d%d", &n, &m);
		Init (n, m);
		SPFA (n);

		tot = SUM (n);
		Uinit (n, m);
		SPFA (n);

		Tot + = SUM (n);
	printf ("%lld\n", tot);
return 0; }

Word Explanation:

Theater:n, cinema, theatre

Invitation:n, invite

Antique:adj, old, ancient

Comedian:n, comedian

PROPAGATE:VT, spread, propaganda

Most of all: most importantly

Comedy:n, Comedy

Invitation Card:n, Invitation letter

PROGRAMME:N, program, Programme, program

Being hired to: employed to do ....

DISTRIBUTE:VT, distribute, scatter

ASSIGN:VT, assign, assign

influence:vt/n, influence, change

Robbery:n, Rob.

Unidirectional:adj, one-way.

Originating:adj, originating from, originating from

DENOTE:VT, said

Fee:n, cost (transportation expenses, etc., such as bus fares)

Table:n, table

On the SPOT:ADV, on the spot, immediately

Payable:adj, to be paid for, to deal with

Round:n, round, loop, round

Checkpoint:n, checkpoint, checkpoints.

Scan:n, scan

Predetermined:adj, set it up beforehand.

MINIMIZE:VT to minimize the amount of the

DESIGNATE:VT, designating, assigning

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.