题干:

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−109≤w≤10^9109, 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.

样例输入复制

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

题目大意:

给一张n个点m条边的带权有向图(权值可能为负),问你依次进行6个操作,每个操作给定起点和终点,让你在起点终点之间加一条边,问你这条边的边权最小是多少,可以保证新图中无负环。

解题报告:

二分权值判负环check就行了。然而标解好像不用二分,直接找到s到t的最短路就可以了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 500 + 5;
const ll PYL = 1e12;
int n,m;
int tot,head[MAX];
struct Edge {int u,v;int ne;ll w;
} e[MAX*100];
void add(int u,int v,ll w) {e[++tot].u = u;e[tot].v = v;e[tot].w = w;e[tot].ne = head[u];head[u] = tot;
}
ll dis[MAX];
int cnt[MAX];
bool vis[MAX];
bool spfa(int st) {for(int i = 1; i<=n+1; i++) dis[i] = 1e12,vis[i] = 0,cnt[i] = 0;;queue<int> q;dis[st]=0;q.push(st);vis[st]=1; cnt[st]=1;while(q.size()) {int cur = q.front();q.pop();vis[cur]=0;for(int i = head[cur]; ~i; i = e[i].ne) {int v = e[i].v;if(dis[v] <= dis[cur] + e[i].w) continue;dis[v] = dis[cur] + e[i].w;if(!vis[v]) {cnt[v]++; vis[v]=1; q.push(v);if(cnt[v] > n) return 0;//}}}return 1;//没有负环返回1
}
int U[MAX],V[MAX],W[MAX];
int main()
{int T;cin>>T;while(T--) {scanf("%d%d",&n,&m);for(int i = 1; i<=m; i++) scanf("%d%d%d",&U[i],&V[i],&W[i]),U[i]++,V[i]++;for(int Q = 1; Q<=6; Q++) {ll l = 0,r = 2e12,ans,mid;int st,ed;scanf("%d%d",&st,&ed);st++,ed++;while(l<=r) {mid = (l+r)>>1;tot=0;memset(head,-1,sizeof head);for(int i = 1; i<=m; i++) add(U[i],V[i],W[i]);for(int i = 1; i<=n; i++) add(n+1,i,0);add(st,ed,mid-PYL);if(spfa(n+1)) ans = mid,r = mid-1;else l = mid+1;}U[++m] = st,V[m] = ed,W[m] = ans-PYL;printf("%lld\n",ans-PYL);}}return 0 ;
}

【2019icpc南京站网络赛 - H】Holy Grail(最短路,spfa判负环)相关推荐

  1. 【2019icpc南京站网络赛 - F】Greedy Sequence(思维,贪心构造,STLset)

    题干: You're given a permutation aa of length nn (1 \le n \le 10^51≤n≤105). For each i \in [1,n]i∈[1,n ...

  2. H - Holy Grail

    H - Holy Grail 题意: 题干又臭又长 我简单说说 n个点,m条有向边,边权为负,然后给你六组起始点(s点和t点),你要在s和t之间建一个有向边,要使得权值最小,问这六组边依次是多少? 不 ...

  3. 2019 ICPC 南昌网络赛 H. The Nth Item

    2019 ICPC 南昌网络赛 H. The Nth Item 题目大意:已知一个数列F(n): F(0)=0,F(1)=1 F(n)=3∗F(n−1)+2∗F(n−2),(n≥2) ​ 给你一个操作 ...

  4. H. Holy Grail(The Preliminary Contest for ICPC Asia Nanjing 2019题解)

    题目链接 As the current heir of a wizarding family with a long history,unfortunately, you find yourself ...

  5. 2019ICPC(南京) - Holy Grail(最短路)

    题目链接:点击查看 题目大意:给出一个图,包括n个点和m条边,然后给出6个查询,每次询问给出两个点u和v,输出v到u的最短路的相反数,并将u到v,权值为刚才求出的答案这条边加入到图中 题目分析:裸的最 ...

  6. HDU - 5875 2016 ACM/ICPC 大连网络赛 H题 暴力

    题目链接 题意:给你一个区间l,r一直将val[l]模上val[l+1],val[l+2]...val[r],因为一个模上比前一个数小数是没有意义的,所以需要将每一个点找到右边第一个小于他的点就行. ...

  7. XKC's basketball team(2019徐州站网络赛E线段树)

    题目链接:题目链接嘤嘤嘤 一开始以为是主席树,想明白了之后线段树就可以.从后往前遍历,线段树二分查找离这个数最远的数字.返回那个数的下标.这个数查找完了就把这个数插入到线段树里. 代码如下: #inc ...

  8. HDU 4812 D Tree (点分治) (2013ACM/ICPC亚洲区南京站现场赛)

    HDU 4812 D Tree 思路 点对距离相等并且要求输出字典序最小的点对,距离相等不就是点分治裸题了嘛, 照着这个思路出发我们只要记录下所有点对是满足要求的,然后再去找字典序最小的点对就行了, ...

  9. 2018ACM-ICPC南京赛区网络赛: B. The writing on the wall

    B. The writing on the wall 题目链接:https://nanti.jisuanke.com/t/30991 题意: 给你一个n*m的矩阵, 其中有些格子上面有障碍物,问有多少 ...

最新文章

  1. 青茶什么时候拆_篮球:挡拆是艺术,绝知要躬行,最简单也是最复杂的篮球战术...
  2. 为何BERT在 NLP 中的表现如此抢眼?
  3. Intellij Idea 导入多个maven项目展示在左侧栏Maven Projects
  4. DNS SOA NS区别
  5. 虚拟机性能测试:八 性能分析—Windows体验指数
  6. 【排序函数讲解】sort-C++
  7. 周小川:数字人民币不会取代美元 也不会威胁全球货币体系
  8. Linux-1:安装忘记密码CRT连接centos 6.5
  9. 如何在 Mac 上使用网络位置?
  10. Linux 程序运行时报错:找不到库文件[cannot open shared object file: No such file or directory ```](转载)
  11. java基本数据类型转类对象
  12. 清明节,我想起了我的外公
  13. Aspose.word Java实现html转word,word转html
  14. 光学元件生产工艺流程
  15. IIS的安装及web服务器配置
  16. iReport —— A4打印,只占纸张的一半,如何解决
  17. 小猿圈IT自学分享-自学编程需要克服的困难
  18. 仿知乎日报图文小程序模板
  19. c语言写一个电脑程序,C语言实现电脑关机程序
  20. Intel Composer XE

热门文章

  1. WF4.0 基础篇 (二十九) WorkflowInspectionServices
  2. [剑指offer][JAVA]面试题第[06]题[从尾到头打印链表][栈][递归]
  3. uci数据集_数据分析找不到数据集?快来看这个盘点
  4. sqlite管理工具_Liquibase 数据库版本管理工具:1.安装
  5. iphone彻底删除照片如何恢复_手机删除的照片如何恢复?OPPO最新照片恢复
  6. threejs精灵(Sprite)
  7. 零基础不建议学前端_web前端培训心得:零基础怎样学好web前端
  8. oracle挂载到空闲进程,Oracle部分后台进程
  9. mysql事务所_mysql事务
  10. mysql skip_counter_mysql的三个故障解决小结