计蒜客题目链接:https://nanti.jisuanke.com/t/41305

给定的起点是S,终点是T,反向跑一下就可以了,注意判负环以及每次查询需要添加边

AC代码:

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
struct node{
vector<int> v;
vector<int> w;
}g[305];
void addedge(int x,int y,int w){
g[x].v.push_back(y);
g[x].w.push_back(w); //建图操作
}
int n,m;
int inq[305],cnt[305],d[305];
bool bellman_ford(int s,int t){
queue<int> Q;
memset(inq,0,sizeof(inq));
memset(cnt,0,sizeof(cnt));
memset(d,inf,sizeof(d));
d[s] = 0;
inq[s] = 1;
Q.push(s);
while(!Q.empty() ){
int u = Q.front() ;
Q.pop();
inq[u] = 0;
for(int i = 0;i<g[u].v.size() ;i++ ){
int e = g[u].w[i];
int v = g[u].v[i];
if(d[u]<inf && d[v] > d[u] + e){
d[v] = d[u] + e;
if(!inq[v]){
Q.push(v);
inq[v] = 1;
if(++cnt[v] > n){
return false;//判定负环,若一个节点入队列超过n次则出现负环
}
}
}
}
}
return true;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i = 0;i<305;i++){
g[i].v.clear() ,g[i].w.clear() ; //清空图
}
for(int i = 1;i<=m;i++){
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
addedge(x,y,w);
}
for(int i = 0;i<6;i++){
int s,t;
scanf("%d%d",&s,&t);
bellman_ford(t,s);
printf("%d\n",-d[s]);
addedge(s,t,-d[s]);//以题意在原图添加新的边
}
}
return 0;
}

2019 ICPC南京站网络赛 H题 Holy Grail(BF算法最短路)的更多相关文章

  1. 2019 ICPC南昌邀请赛网络赛比赛过程及题解

    解题过程 中午吃饭比较晚,到机房lfw开始发各队的账号密码,byf开始读D题,shl电脑卡的要死,启动中...然后听到谁说A题过了好多,然后shl让blf读A题,A题blf一下就A了.然后lfw读完M ...

  2. 2015年ACM-ICPC亚洲区域赛合肥站网络预选赛H题——The Next (位运算)

    Let L denote the number of 1s in integer D's binary representation. Given two integers S1 and S2, we ...

  3. 2018 ACM南京网络赛H题Set解题报告

    题目描述 给定\(n\)个数$a_i$,起初第\(i\)个数在第\(i\)个集合.有三种操作(共\(m\)次): 1 $u$ $v$ 将第$u$个数和第$v$个数所在集合合并 2 $u$ 将第$u$个 ...

  4. 2018 ACM-ICPC徐州站网络赛 G题

    There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xxx , yy ...

  5. ICPC青岛站网络赛-C-高效模拟

    嗯这道辣鸡题,当时我队友写了错误的代码,我稍微改动了,思路基本上是对了,但是就是超时,我第一直觉是我这个算法思路是没有任何问题的,但是就是TLE,我感觉这个算法已经优化的不能再优化了啊...后面就怀疑 ...

  6. ACM-ICPC 2018青岛网络赛-H题 Traveling on the Axis

    题目:略(不知道怎么从ZOJ搬题) 地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4054 把这题的每个点分成两种情况 ...

  7. 2015北京网络赛 H题 Fractal 找规律

    Fractal Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc2015beijingo ...

  8. 2013长沙网络赛H题Hypersphere (蛋疼的题目 神似邀请赛A题)

    Hypersphere Time Limit: 1 Second       Memory Limit: 32768 KB In the world of k-dimension, there's a ...

  9. hdu 5874 Friends and Enemies icpc大连站网络赛 1007 数学

    #include<stdio.h> #include<iostream> #include<algorithm> #include<math.h> #i ...

随机推荐

  1. Codeforces 577A - Multiplication Table

    Let's consider a table consisting of n rows and n columns. The cell located at the intersection of i ...

  2. AspxGridView 客户端点击获取对应的列值

    Html 内容: <dx:ASPxGridView ID="ASPxGridViewCluster" runat="server" Width=" ...

  3. c语言 printf格式化输出

    #include <iostream> #include<stdio.h> #include <cstring> using namespace std; int ...

  4. Java安装及环境配置

    一.jdk安装及环境配置 1. 下载jdk 去oracle官网下载,这里使用的jdk版本为 有一个需要注意的问题就是7u71后的jdk有两个版本,奇数版本为无BUG版,偶数版包含奇数版全部内容但是有一 ...

  5. Vue 嵌套路由使用总结

    Vue 嵌套路由使用总结   by:授客 QQ:1033553122   开发环境   Win 10   node-v10.15.3-x64.msi 下载地址: https://nodejs.org/ ...

  6. markdown文本编辑学习笔记2

    目录 1.删除线 2.无序列表 4 todo list 5分割符号 6 TOC自动生成目录 7 插入代码块 8 斜体.粗体.删除线.下划线.背景高亮 markdown编辑公式 1.删除线 ~~要删除的 ...

  7. python UI自动化之处理多窗口

    前言 有些页面的链接打开后,会重新打开一个窗口,想要在新页面上操作,就需要先切换窗口了.获取窗口的唯一标识用句柄表示,所以只需要切换句柄,我们就能在多个页面上灵活自如的操作了. 1.元素有属性,浏览器 ...

  8. 题解【洛谷P3958】[NOIP2017]奶酪

    题面 题解 我们考虑使用一个并查集维护空洞之间的关系. 如果两个空洞能相互到达,那么它们的祖先也是相同的. 枚举从哪一个空洞开始,能否到达奶酪的上表面. 如果能到达就输出Yes,否则输出No. 注意开 ...

  9. linux 扩展内存

    一.逻辑卷创建使用 https://www.cnblogs.com/xiaoluo501395377/archive/2013/05/24/3096087.html fdisk -l pvcreate ...

  10. JS 获取随机颜色值

    获取随机颜色值 function fn1(){ return '#' + Math.floor( Math.random() * 0xffffff ).toString(16); } function ...