HDU——1535 Invitation Cards (spfa)

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

题意:求从1号点到各个点来回的最小距离之和。图是有向的。

题解:先建图,从一号点到各个点跑一边(大部分人都能想到吧),然后可能会想从每个点到1号点再跑一遍,这样结果对,但是超时,怎么办呢,我们反向建图从1号点跑一边就可以了。为什么呢?请看解释:1——>2为10,2——>1为20 这样从一号点正向跑完为10,然后我们反向建图2——>1为10,1——>2为20这样从一号点跑到2号点为20也就是刚开始从2——>1的长度,这样我们就可以发现只要反着建图再从1号点跑一边,到每个点的最短路,就是从那个点到1号点的最短路,这样我们就不用每个点都跑一半了。请看代码。

先看超时代码:

#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int MAX=100005;
const int inf=0x3f3f3f3f;
int n,m,x;
struct  hh{
	int u;
	int v;
	int w;
	friend bool operator<(hh a,hh b)
    {
        return a.w>b.w;
    }
};
vector <hh> vs[MAX];
int dis[MAX],vis[MAX];
void add(int u,int v,int w){
	hh tmp;
	tmp.u=u;
	tmp.v=v;
	tmp.w=w;
	vs[u].push_back(tmp);
}
void init(){
	memset(dis,inf,sizeof(dis));
	memset(vis,0,sizeof(vis));
}
void Dijkstra(int x,int y){
	priority_queue<hh> q;
	dis[x]=0;
	hh tmp1,tmp2;
	tmp1.v=x;
	tmp1.w=0;
	q.push(tmp1);
	while(!q.empty()){
		tmp1=q.top();
		q.pop();
		if(vis[tmp1.v])	continue;
		vis[tmp1.v]=1;
		if(tmp1.v==y) break;
		for (vector<hh>::iterator it=vs[tmp1.v].begin();it!=vs[tmp1.v].end();it++){
			if(vis[it->v]) continue;
			if(dis[it->v]>dis[tmp1.v]+it->w){	
				tmp2.v=it->v;
				tmp2.w=dis[tmp1.v]+it->w;
				dis[it->v]=tmp2.w;
				q.push(tmp2);
			}
		}
	}
}
int main(){
	int t;
	cin >> t;
	while(t--){
		while(cin >> n >> m){
	        init();
	        for (int i = 0; i <= n;i++){
	        	vs[i].clear();
			}
		    int sum=0; 
	        for (int i = 0; i < m;i++){
		        int u,v,t;
		        cin >> u >> v >> t;
		        add(u,v,t);
    	    }
    	    Dijkstra(1,100000);// 从1号点跑一遍
	        for (int i = 1; i <= n;i++){
		        sum+=dis[i];
    	    }
	        for (int i = 1; i <= n;i++){
		        Dijkstra(i,1);//从每个点到一号点跑一边    超时!!!
		        sum+=dis[1];
		        init();
	        }
	        cout << sum << endl;
	    }	
	}
	
	return 0;
} 

AC代码:

#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int MAX=1e6+20;
const int inf=0x3f3f3f3f;
vector<pair<int,int> > e[MAX];// 存图常用方法
int dis[MAX],inq[MAX],n;
int a[MAX][4];
void SPFA(int s){ // spfa模板 
	memset(dis,inf,sizeof(dis));//fill(dis,dis+n+1,inf);//赋初值
	memset(inq,0,sizeof(inq));
	queue<int> q;
	dis[s]=0;
	q.push(s);
	inq[s]=1;
	while(!q.empty()){
		int u=q.front();
		q.pop();
		inq[u]=0;
		for(int i = 0;i < e[u].size();i++){
			int v=e[u][i].first;
			int w=e[u][i].second;
			if(dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				if(!inq[v]){
					q.push(v);
					inq[v]=1;
				}
			}
		}
	}
}
int main(){
	int t;
	cin >> t;
	while(t--){
		int m;
		cin >> n >> m;
		for(int i = 1;i <= n;i++){
			e[i].clear();
		}
		for(int i = 1;i <= m;i++){
			int u,v,w;
			cin >> u >> v >> w;
			e[u].push_back(make_pair(v,w));//建立正向图
			a[i][0]=v,a[i][1]=u,a[i][2]=w;// 存下来方便建立反向图
		}	
		int ans=0;		
		SPFA(1); // 从一开始找 
		for(int i = 1;i <= n;i++){
			ans+=dis[i];
			e[i].clear();// 注意:要清空正向图 
		}
		for(int i = 1;i <= m;i++){
			e[a[i][0]].push_back(make_pair(a[i][1],a[i][2])); // 建立反向图 
		}
		SPFA(1); // 从一开始找 
		for(int i = 1;i <= n;i++){
			ans+=dis[i];
		}
		cout << ans << endl;
	}
	return 0;
}

突然发现spfa跟Dijkstra好像啊!!是我太水,不懂吗???

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值