Hduoj1535【SPFA】

/*Invitation Cards
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2921    Accepted Submission(s): 1367


Problem Description
In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery. 
The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan. 

All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees. 


 

Input
The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop. 

 

Output
For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers. 

 

Sample Input
2
2 2
1 2 13
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50
 

Sample Output
46
210
 

Source
Central Europe 1998 
*/ 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define INF 99999999
int N, n, m;
int first[1000010],next[1000010], que[1000010],d[1000010];
bool vis[1000010];
struct road
{
	int u, v, w;
}R[1000010];

void spfa()
{
	for(int i = 0; i <= n; ++i)
	d[i] = INF;
	d[1] = 0;
	int front = 0, rear = 0;
	que[rear++] = 1;
	memset(vis, 0, sizeof(vis));
	while(front < rear)
	{
		int u = que[front++];
		vis[u] = false;//出隊 
		//從該結點引出的最後一條邊開始  
		for(int k = first[u]; k != -1; k = next[k])
		{
			int v = R[k].v;
			if(d[v] > d[u] + R[k].w)//更新最短距離 
			{
				d[v] = d[u] + R[k].w; 
				if(!vis[v])     //將更新的點加入隊列以便後繼更新  
				{
					que[rear++] = v;
					vis[v] = true;
				}
			}
		}
	}
}

//逆向構圖 
void rebuild()
{
	memset(first, -1, sizeof(first));
	memset(next, -1, sizeof(next));
	for(int i = 0; i < m; ++i)
	{
		int v = R[i].v;
		R[i].v = R[i].u;
		R[i].u = v;
		next[i] = first[v];
		first[v] = i;
	}
} 

int main()
{
	int i, j, k, ans;
	scanf("%d", &N);
	while(N--)
	{
		memset(first, -1, sizeof(first));
		memset(next, -1, sizeof(next));
		scanf("%d%d", &n, &m);
		for(i = 0; i < m; ++i)
		{
			//保存 u結點所引出的邊的編號 
			scanf("%d%d%d", &R[i].u, &R[i].v, &R[i].w);
			next[i] = first[R[i].u];//剛開始爲-1即沒有  否則將保存u結點在本次出度邊之前的最後一條出度邊的編號 下面更新保存本次出度邊 
			first[R[i].u] = i;//first保存的是u結點的最後一條出度邊 
		}
		ans = 0;
		spfa();
		for(i = 2; i <= n; ++i)
		ans += d[i];
		rebuild();
		spfa(); 
		for(i = 2; i <= n; ++i)
		ans += d[i];
		printf("%d\n", ans);
	}
	return 0;
}


題意:有n個汽車站,每個車站都需要一個學生站崗,每個學生都從1號車站出發到達其他汽車站,然後再從相應的汽車站返回1號汽車站,每個車站到達其他車站都有相應的路線和路費,現在要求計算學生們所需的最小的資金。

思路:這裏有2個最小的路徑,一個是1號車站到其他車站的最小路徑,一個是其他車站到1號車站的最小路徑,然而2個車站之間來回的消費是不同的,所以要進行2次的spfa算法,其中一次需要對圖的路徑進行反向構圖並交換車站,再用spfa即可求出第二種路徑的消費。這裏最難的便是構圖,由於百萬的數據量所以無法用鄰接表來保存兩兩之間的費用,然而這裏的邊卻最多隻有一百萬條,我們可以根據邊來統計車站到車站之間的距離(具體可以看代碼理解)。

發佈了250 篇原創文章 · 獲贊 3 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章