Holy Grail(spfa最短路)

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≤51 \le t \le 51≤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,y<n0 \le x,y<n0≤x,y<n,−109-10^9−109≤w≤10910^9109, x=yx \not = yx​=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 666 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

题意描述:就是通过单源路找到从一点到另一点的最少代价,总共建立6条边先建立的边可以在后面建立的时候用到,找到建立每条路的最小值(可以为负值);

解题思路:可能对算法不是太熟悉的缘故想了很久才想到要用Bellmen-Ford法的;但是刚开始没有注意到先建立的路在后面也能用,再加上本来的题意没有全部理解,导致用了正确的算法还是没有做出来这个题,知道今天知道了Spfa算法才算是写出来的;

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int dis[510];
int vis[510];
int map[510][510];
int m,n;
int inf=99999999;
int spfa(int x,int y)
{
    queue<int> q;
	
	int i;
	for(i=0;i<n;i++)
	{
		dis[i]=inf;
		vis[i]=0;
	}
	
	dis[x]=0;
	vis[x]=1;
	q.push(x);
	while(!q.empty())
	{
		//printf("**");
		int u=q.front();//取出队首
	//	printf("%d**\n",u);
		q.pop();
		
		vis[u]=0;
		for(i=0;i<n;i++)
		{
			if(dis[i]>dis[u]+map[u][i])
			{
				dis[i]=dis[u]+map[u][i];
				if(vis[i]==0)
				{
					vis[i]=1;
					q.push(i);
				}
			}
		}
	}
	return dis[y];
}
int main()
{
	int i,j,k,x,y,t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(i=0;i<n;i++)
		  for(j=0;j<n;j++)
		  {
		  	if(i==j) map[i][j]=0;
		  	else map[i][j]=99999999;
		  }
		int a,b,c;
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			map[a][b]=c;
		}
		for(i=0;i<6;i++)
		{
			scanf("%d%d",&x,&y);
			
			int sum=-spfa(y,x);
			map[x][y]=sum;
			printf("%d\n",sum);
		}
	}
	return 0;
}

 

  • 30
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 30
    评论
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

BayMin0520

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值