最短路算法之SPFA算法

最短路算法之SPFA算法

    • H题签到题
      • 题面
      • 思路
        • 附spfa算法:
      • 源码

9.1南昌网络赛

H题签到题

题面

As the current heir of a wizarding family with a long history,unfortunately, you find yourself forced to participate in the cruel Holy Grail War which has a reincarnation of sixty years.However,fortunately,you summoned a Caster Servant with a powerful Noble Phantasm.When your servant launch her Noble Phantasm,it will construct a magic field,which is actually a directed graph consisting of n vertices and m edges.More specifically,the graph satisfies the following restrictions :

Does not have multiple edges(for each pair of vertices x and y, there is at most one edge between this pair of vertices in the graph) and does not have self-loops(edges connecting the vertex with itself).
May have negative-weighted edges.
Does not have a negative-weighted loop.
n<=300 , m<=500.
Currently,as your servant’s Master,as long as you add extra 6 edges to the graph,you will beat the other 6 masters to win the Holy Grail.

However,you are subject to the following restrictions when you add the edges to the graph:

Each time you add an edge whose cost is c,it will cost you c units of Magic Value.Therefore,you need to add an edge which has the lowest weight(it’s probably that you need to add an edge which has a negative weight).
Each time you add an edge to the graph,the graph must not have negative loops,otherwise you will be engulfed by the Holy Grail you summon.
Input
Input data contains multiple test cases. The first line of input contains integer t — the number of testcases (1≤t≤5).

For each test case,the first line contains two integers n,m,the number of vertices in the graph, the initial number of edges in the graph.

Then m lines follow, each line contains three integers x, y and w (0≤x,y9≤w≤109, x ≠ y) denoting an edge from vertices x to y (0-indexed) of weight w.

Then 6 lines follow, each line contains two integers s,t denoting the starting vertex and the ending vertex of the edge you need to add to the graph.

It is guaranteed that there is not an edge starting from s to t before you add any edges and there must exists such an edge which has the lowest weight and satisfies the above restrictions, meaning the solution absolutely exists for each query.

Output
For each test case,output 66 lines.

Each line contains the weight of the edge you add to the graph.

样例输入

1
10 15
4 7 10
7 6 3
5 3 3
1 4 11
0 6 20
9 8 25
3 0 9
1 2 15
9 0 27
5 2 0
7 3 -5
1 7 21
5 0 1
9 3 16
1 8 4
4 1
0 3
6 9
2 1
8 7
0 4

样例输出

-11
-9
-45
-15
17
7

思路

使用spfa算法依次求出给出的六组点,反向(例如样例:添加4到1的边则求1到4的最短路)之间的最短路,取反即为答案,每求出一个还需把该边插入

附spfa算法:

可以求从源点到任意点的最短路(且权可负),利用队列进行实现,用vi[N]保存源点到各点的最短距离,初始值为极大值,源点到本身距离为0。初始时将源点加入队列,当队列不空时,每次从队头弹出一个点,处理其能到达的所有边,if(vi[end]>vi[begin]+w(begin,end)) 则更新vi[end],并把end点加入队列;循环进行处理,直到队列为空。(可处理无负环的情况)

源码

#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
int n,m;
const int inf=1e12;
const int N=330;
const int M=550;
typedef struct{
	int h[N];
	ll w[N];
	int n=0;
}Node;
ll spaf(int be,int en,Node e[]){//核心代码 
	ll vi[N];//每个点到源点的距离 
	//memset(vi,1,sizeof(vi));
	for(int i=0;i<n+5;i++) vi[i]=inf;
	vi[be]=0;
	queue<int> q;
	q.push(be);
	while(!q.empty()){
	int i,a=q.front();q.pop();
	for(i=0;i<e[a].n;i++){
		if(vi[e[a].h[i]]>vi[a]+e[a].w[i]) {//可以进行松弛操作 
			vi[e[a].h[i]]=vi[a]+e[a].w[i];
			q.push(e[a].h[i]);
		}
	}
	}
	return vi[en];
}
int main(int argc, char** argv) {
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int t;cin>>t;
	while(t--){
		Node e[N]; 
		int begin;cin>>n>>m;
		while(m--){
			cin>>begin;
			cin>>e[begin].h[e[begin].n]>>e[begin].w[e[begin].n];
			e[begin].n++;
		}
		for(int i=0;i<6;i++){
			int be,en;ll a,b,w;
			cin>>be>>en;
			//a=spaf(be,en,e);
			//b=spaf(en,be,e);
			//w=a
			w=spaf(en,be,e);//添加be→en的边,则判断en→be的最短路是多少 
			w*=-1;
			cout<<w<<'\n';
			e[be].h[e[be].n]=en;e[be].w[e[be].n]=w;//添加 be→en的边
			e[be].n++;
		}
	}
	return 0;
}
/*
1
10 15
4 7 10
7 6 3
5 3 3
1 4 11
0 6 20
9 8 25
3 0 9
1 2 15
9 0 27
5 2 0
7 3 -5
1 7 21
5 0 1
9 3 16
1 8 4
4 1
0 3
6 9
2 1
8 7
0 4
*/ 

你可能感兴趣的:(最短路算法之SPFA算法)