题意:给出一个n个点,m条边的有向图,再给6组询问,在u,v间建一条权值最小的边,使图中没有负环。保证有解。
做法:u,v加边,如果有负环必定过u,v。以v为起点,到u的最短路和u->v组成的环权值是0时,u->v最小,且无负环。
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
typedef long long LL;
#define MAXX 2005
struct E {
int to,next;
LL w;
}g[MAXX];
int front[MAXX],tot;
void Add(int from,int to,LL w) {
g[++tot].to = to;
g[tot].w = w;
g[tot].next = front[from];
front[from] = tot;
}
int n,m;
LL d[MAXX];
int inq[MAXX];
void spfa(int x) {
memset(d,127/3,sizeof(d[0])*(n+3));
d[x] = 0;
inq[x] = 1;
queue<int> q;
q.push(x);
while(q.size()) {
int t = q.front();
q.pop();
inq[t] = 0;
for (int i = front[t]; i; i = g[i].next) {
int to = g[i].to;
if (d[to] > d[t]+g[i].w) {
d[to] = d[t]+g[i].w;
if (!inq[to]) {
inq[to] = 1;
q.push(to);
}
}
}
}
}
int main() {
int T; cin >> T;
while (T--) {
tot = 0;
memset(front,0,sizeof(front));
int u,v,w;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> u >> v >> w;
u++; v++;
Add(u,v,w);
}
for (int i = 1; i <= 6; i++) {
cin >> u >> v;
u++; v++;
spfa(v);
printf("%lld\n",-d[u]);
Add(u,v,-d[u]);
}
}
return 0;
}
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 \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 \le x,y<n0≤x,y<n,-10^9−10
9 ≤w≤10^910 9, x \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 66 lines.
Each line contains the weight of the edge you add to the graph.