题目链接

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,y<n,-10^9≤w≤10^9, 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 666 lines.

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

INPUT
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
OUTPUT
-11
-9
-45
-15
17
7

题意:n个点m条边,给出m条边的起点终点和权值,最后给出6组数x,y,要求在x到y上加权值,问所加权值最小为多少(加边满足加完之后不能有负环,加完立即生效)。

思路:直接找y到x的最短路取相反值即可,因为这样不会有负环。因为有负边,所以用SPFA。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<set>
#include<map>
#include<string>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#define maxn 150000
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
ll dis[500];
int vis[500];
struct A
{int to;ll w;
};
vector <A> v[500];
void SPFA(ll s)
{queue <int> q;memset(dis,inf,sizeof(dis));memset(vis,0,sizeof(vis));dis[s]=0;vis[s]=1;q.push(s);while(!q.empty()){int u=q.front();q.pop();vis[u]=0;for(int i=0;i<v[u].size();i++){int x=v[u][i].to;ll y=v[u][i].w;if(dis[u]+y<dis[x]){dis[x]=dis[u]+y;if(vis[x]==0){q.push(x);vis[x]=1;}}}}return ;
}int main()
{int t;scanf("%d",&t);while(t--){int n,m,a,b;ll c;struct A t;scanf("%d%d",&n,&m);for(int i=1;i<=m;i++){scanf("%d %d %lld",&a,&b,&c);t.to = b;t.w =c;v[a].push_back(t);}for(int i=0;i<6;i++){scanf("%d %d",&a,&b);SPFA(b);ll ans = dis[a];t.to = b;t.w = (-1)*ans;v[a].push_back(t);printf("%lld\n",(-1)*ans);}for(int i=0;i<=n;i++){v[i].clear();}}return 0;}

H. Holy Grail(The Preliminary Contest for ICPC Asia Nanjing 2019题解)相关推荐

  1. F. Greedy Sequence(The Preliminary Contest for ICPC Asia Nanjing 2019题解)

    题目链接 You're given a permutation aaa of length n (1≤n≤105). For each i∈∈[1,n], construct a sequence s ...

  2. The Preliminary Contest for ICPC Asia Nanjing 2019 B. super_log (广义欧拉降幂)

    In Complexity theory, some functions are nearly O(1)O(1), but it is greater then O(1)O(1). For examp ...

  3. The Preliminary Contest for ICPC Asia Nanjing 2019ICPC南京网络赛

    B.super_log (欧拉降幂) •题意 定一个一个运算log*,迭代表达式为 给定一个a,b计算直到迭代结果>=b时,最小的x,输出对m取余后的值 •思路 $log*_{a}(a^{a}) ...

  4. The Preliminary Contest for ICPC Asia Xuzhou 2019

    小   银   川,给大爷们AK穿了 比赛地址:https://www.jisuanke.com/contest/3005?view=challenges A.(待补) B.1e9的数据范围,一开始从 ...

  5. The Preliminary Contest for ICPC Asia Shanghai 2019 B. Light bulbs(卡了线段树空间的思维题)

    传送门:https://nanti.jisuanke.com/t/41399 题目描述 There are NNN light bulbs indexed from 000 to N−1N−1N−1. ...

  6. The Preliminary Contest for ICPC Asia Shanghai 2019 BDL

    传送门 B:离散化+差分 #include <bits/stdc++.h> using namespace std; typedef long long ll; struct node{i ...

  7. The Preliminary Contest for ICPC Asia Xuzhou 2019 - C Buy Watermelon | 读题

    They want to cut the watermelon in two parts, and each part weighs two times as much as a kilogram . ...

  8. The Preliminary Contest for ICPC Asia Xuzhou 2019【B. so easy】(set 解法 与正解 unordered_map+并查集)

    so easy 题目大意 给你两个操作,op=1时,将那个数不可用,op=2时进行查询功能 找到从x开始第一个存在的数 非官方题解 set 内置红黑树,有默认从小到大排序功能,所以我们可以暴力一发,不 ...

  9. [The Preliminary Contest for ICPC Asia Xuzhou 2019 - 徐州网络赛E] XKC's basketball team

    XKC's basketball team XKC , the captain of the basketball team , is directing a train of nn team mem ...

最新文章

  1. Ubuntu10下MySQL搭建Amoeba系列(文章索引)
  2. mongoDB简明教程-python
  3. RabbitMQ批量确认发布
  4. jest java_使用JestClient操作ElasticSearch的简单demo
  5. 数据结构与算法快慢指针
  6. html 列导航包括导航,在HTML5中,主导航应该在元素内部还是外部?
  7. 苏宁11.11:苏宁易购移动端的架构优化实践
  8. vb.net 模拟鼠标 失去焦点_DNF:都9102年了还在手动点竹子?鼠标连点器了解一下...
  9. pr2020lut导入_PS PR AE怎么批量导入LUTS调色预设
  10. 电工电子技术计算机用学吗,电工电子技术第4版学习指导与习题解答(电子电气基础课程规划教材)...
  11. 学习纹理格式(DXGI_FORMAT 和 VkFormat)
  12. python爬虫基础详解
  13. JavaScript高程三----(基础一)
  14. 核心网CN | IMSI、TMSI、P-TMSI、GUTI、S-TMSI、MSISDN、MSRN、IMEI等这些移动用户标识的辨析
  15. isfinite函数_isfinite()函数以及C ++中的示例
  16. 【094】统计大写字母个数
  17. 推荐3个搜索资源的网站,保存起来,用的时候方便找哦
  18. 漫步数学分析十九——介值定理
  19. word中添加续表和合并续表
  20. 排查https请求出现received fatal alert: internal_error的问题

热门文章

  1. 二手交易app manifest.xml
  2. HyperLedger Fabric 查询机制
  3. 用Python做数据分析之数据筛选及分类汇总
  4. 苹果cms修改服务器,购买m1938工作室制作的苹果cms模板如何安装修改苹果cms页面说明...
  5. fastadmin 自定义按钮 btn-ajax 执行成功后 自动刷新
  6. oCPC和oCPM的本质区别是什么?
  7. A段架构设计_隽语集(Business Thinking _1201)
  8. 为什么地球上的第一个复杂生命体出现在海洋中
  9. c语言怎么让程序停止3秒,c语言暂停语句1秒
  10. 第二章 Silicon labs EFR32 MG21 验证蓝牙的私有Characteristic的读/写