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.
题意:给你n个点,m条边的图,边的权值有正有负。并且原图没有重边和负环,现在给给你六个顶点和终点,让你添加六条边,并且要保证添加后图无负环,输出添加的每条边的最小值
思路:spfa纪录起点终点间的最短距离,然后给他们添加一条边,权值是最短距离取负值
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn = 505;
const ll INF = 0x3f3f3f3f;
vector <pair<int,int> > E[maxn];
struct P
{
ll x,y;
} p[100];
ll n,m;
ll d[maxn],inq[maxn];
//d[maxn]起点到终点的距离,inq[maxn]记录这个东西是否在队列里
void init()
{
for(ll i=0; i<maxn; i++)
E[i].clear();
memset(inq,0,sizeof(inq));
memset(d,0x3f,sizeof(d));
}
void spfa(ll s)
{
memset(d,0x3f,sizeof(d));
memset(inq,0,sizeof(inq));
queue<ll> Q;
Q.push(s),d[s]=0,inq[s]=1;
while(!Q.empty())
{
ll now = Q.front();
Q.pop();
inq[now]=0;
for(ll i=0; i<E[now].size(); i++)
{
ll v = E[now][i].first;
if(d[v]>d[now]+E[now][i].second)
{
d[v]=d[now]+E[now][i].second;
if(inq[v]==1)
continue;
inq[v]=1;
Q.push(v);
}
}
}
}
int main()
{
// freopen("input.txt","r",stdin);
ll t;
scanf("%lld",&t);
while(t--)
{
cin>>n>>m;
init();
for(ll i=0; i<m; i++)
{
ll x,y,z;
scanf("%lld%lld%lld",&x,&y,&z);
E[x].push_back(make_pair(y,z));
//E[y].push_back(make_pair(x,z));
}
for(ll i=1;i<=6;i++)
{
scanf("%lld %lld",&p[i].x,&p[i].y);
}
for(int i=1;i<=6;i++)
{
spfa(p[i].y);
ll sum=-d[p[i].x];
E[p[i].x].push_back(make_pair(p[i].y,sum));
printf("%lld\n",sum);
}
}
return 0;
}