POJ-1511 Invitation Cards (spfa | Dijkstar最小堆 多重境界优化)

题意:共有n个站点,为了宣传效果,每个站点必须有一个人(初始点站不站其实一样),给出m条单向路径和行走该段路径的费用cost,计算所有人来回车费的最小值。

思路:计算一遍从源点出发到所有点的最短路径,相加。再反向计算一遍所有点到源点的最短路径,全部相加显然就是答案。

但需注意,1<=n,m<=1000000,百万的数量级如果不优化,直接使用Dijkstar(n^2),会TLE,作者先后尝试了多种优化方式,详情如下:

One:利用vector充当邻接表,Dijkstar+优先队列,险些超时

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
const int maxn=1000005;
struct Edge{
	int adj,w;
	Edge(){}
	Edge(int a,int b):adj(a),w(b){};	
};
vector<struct Edge > cnt[maxn],tnc[maxn];
int n,m,vis[maxn];
LL dist[maxn],ans;
struct cmp{
	bool operator()(const int a,const int b){
		return dist[a]>dist[b];
	}
};
void Dijkstar1(){
	memset(vis,0,sizeof vis);
	priority_queue<int,vector<int >,cmp> que;
	for(int i=2;i<=n;i++)	dist[i]=INF;
	dist[1]=0; que.push(1);
	while(!que.empty()){
		int u=que.top(); vis[u]=1; que.pop();
		int s=cnt[u].size();
		for(int i=0;i<s;i++){
			int v=cnt[u][i].adj;
			int w=cnt[u][i].w;
			if(!vis[v] && dist[v]>dist[u]+w){
				dist[v]=dist[u]+w;
				que.push(v);
			}
		
		}
	} 
}
void Dijkstar2(){
	memset(vis,0,sizeof vis);
	priority_queue<int,vector<int >,cmp> que;
	for(int i=2;i<=n;i++)	dist[i]=INF;
	dist[1]=0; que.push(1);
	while(!que.empty()){
		int u=que.top(); vis[u]=1; que.pop();
		int s=tnc[u].size();
		for(int i=0;i<s;i++){
			int v=tnc[u][i].adj;
			int w=tnc[u][i].w;
			if(!vis[v] && dist[v]>dist[u]+w){
				dist[v]=dist[u]+w;
				que.push(v);
			}
		
		}
	} 
}

int main(){
	int v1,v2,w;
	int _;cin>>_;
	while(_--){	
		memset(cnt,0,sizeof cnt);
		memset(tnc,0,sizeof tnc);
	  	ans=0;
		cin>>n>>m;
		for(int i=1;i<=m;i++){
			scanf("%d %d %d",&v1,&v2,&w);
			cnt[v1].push_back(Edge(v2,w));
			tnc[v2].push_back(Edge(v1,w));
		}	
		Dijkstar1();
		for(int i=1;i<=n;i++){
			ans+=dist[i];
		}
		Dijkstar2();
		for(int i=1;i<=n;i++){
			ans+=dist[i];
		}
		cout<<ans<<endl;
	}
	return 0;
}

Two:数组链表+Dijkstar+优先队列 还行

#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<algorithm>
#define llong long long
using namespace std;
const int N=1000005;
const int inf=1e10;
int n,m;
int a[N][3];
struct
{
	int w,v,next;
}edge[N];
int edgehead[N];
llong d[N];
bool vis[N];
int k;
void addedge(int u,int v,int w)
{
	edge[k].v=v;
	edge[k].w=w;
	edge[k].next=edgehead[u];
	edgehead[u]=k++;
}
struct cmp/
{
	bool operator()(const int a,const int b)
	{
		return d[a]>d[b];
	}
};
llong dijstra()
{
	memset(vis,0,sizeof(vis));
	priority_queue<int,vector<int>,cmp> que;
	for(int i=2;i<=n;i++)
		d[i]=inf;
	d[1]=0;
	que.push(1);
	while(!que.empty())
	{
		int u=que.top();
		vis[u]=true;
		que.pop();
		for(int i=edgehead[u];i;i=edge[i].next)
		{
			int v=edge[i].v;
			int w=edge[i].w;
			if(!vis[v]&&d[v]>d[u]+w)先。
			{
				d[v]=d[u]+w;
				que.push(v);
			}
		}
	}
	llong ans=0;
	for(int i=1;i<=n;i++)
		ans+=d[i];
	return ans;
}
int main()
{
	int cases;
	scanf("%d",&cases);
	while(cases--)
	{
		k=1;
		memset(edgehead,0,sizeof(edgehead));
		memset(edge,0,sizeof(edge));
		scanf("%d%d",&n,&m);
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d%d",a[i],a[i]+1,a[i]+2);
			addedge(a[i][0],a[i][1],a[i][2]);
		}
		llong ans=dijstra();
		k=1;
		memset(edgehead,0,sizeof(edgehead));
		memset(edge,0,sizeof(edge));
		for(int i=1;i<=m;i++)
		{
			addedge(a[i][1],a[i][0],a[i][2]);
		}
		ans+=dijstra();
		printf("%lld\n",ans);
	}
	return 0;
}

Three:数组链表+spfa+双端队列LLL优化 

Tips:spfa是Bellman-Ford算法的优先队列实现,相关spfa算法与优化可自行查阅学习

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
const int maxn=1000005;
int n,m,k,vis[maxn],Edgehead[maxn],a[maxn][3];
LL dist[maxn];
struct Edge{int v,w,next;}e[maxn];
void addEdge(int u,int v,int w){
	e[k].v=v;e[k].w=w;
	e[k].next=Edgehead[u];Edgehead[u]=k++;
}
LL spfa(){
	memset(vis,0,sizeof vis);
	for(int i=2;i<=n;i++) dist[i]=INF; dist[1]=0;
	deque<int > que; que.push_front(1);
	while(!que.empty()){
		int u=que.front(); que.pop_front(); vis[u]=0;
		for(int i=Edgehead[u];i;i=e[i].next){
			int v=e[i].v,w=e[i].w;
			if(dist[u]+w<dist[v]){
				dist[v]=dist[u]+w;
				if(!vis[v]){
					vis[v]=1;
					if(!que.empty() && dist[v]<dist[que.front()]){
						que.push_front(v);
					}else que.push_back(v);
					
				}
				
			}
		}
	
	}
	LL res=0;
	for(int i=1;i<=n;i++) res+=dist[i];
	return res;
}
int main(){
	int _;cin>>_;
	while(_--){
		memset(Edgehead,0,sizeof Edgehead);
		memset(e,0,sizeof e);
		scanf("%d%d",&n,&m);
		k=1;
		for(int i=0;i<m;i++){
			scanf("%d%d%d",&a[i][0],&a[i][1],&a[i][2]);
			addEdge(a[i][0],a[i][1],a[i][2]);
		}
		LL ans=spfa();
		memset(Edgehead,0,sizeof Edgehead);
		memset(e,0,sizeof e);
		k=1;
		for(int i=0;i<m;i++) addEdge(a[i][1],a[i][0],a[i][2]);
		ans+=spfa();
		printf("%lld\n",ans);
	}
	return 0;
}

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
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值