2019 ICPC南京站网络赛 H题 Holy Grail(BF算法最短路)
计蒜客题目链接: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算法最短路)的更多相关文章
- 2019 ICPC南昌邀请赛网络赛比赛过程及题解
解题过程 中午吃饭比较晚,到机房lfw开始发各队的账号密码,byf开始读D题,shl电脑卡的要死,启动中...然后听到谁说A题过了好多,然后shl让blf读A题,A题blf一下就A了.然后lfw读完M ...
- 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 ...
- 2018 ACM南京网络赛H题Set解题报告
题目描述 给定\(n\)个数$a_i$,起初第\(i\)个数在第\(i\)个集合.有三种操作(共\(m\)次): 1 $u$ $v$ 将第$u$个数和第$v$个数所在集合合并 2 $u$ 将第$u$个 ...
- 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 ...
- ICPC青岛站网络赛-C-高效模拟
嗯这道辣鸡题,当时我队友写了错误的代码,我稍微改动了,思路基本上是对了,但是就是超时,我第一直觉是我这个算法思路是没有任何问题的,但是就是TLE,我感觉这个算法已经优化的不能再优化了啊...后面就怀疑 ...
- ACM-ICPC 2018青岛网络赛-H题 Traveling on the Axis
题目:略(不知道怎么从ZOJ搬题) 地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4054 把这题的每个点分成两种情况 ...
- 2015北京网络赛 H题 Fractal 找规律
Fractal Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc2015beijingo ...
- 2013长沙网络赛H题Hypersphere (蛋疼的题目 神似邀请赛A题)
Hypersphere Time Limit: 1 Second Memory Limit: 32768 KB In the world of k-dimension, there's a ...
- hdu 5874 Friends and Enemies icpc大连站网络赛 1007 数学
#include<stdio.h> #include<iostream> #include<algorithm> #include<math.h> #i ...
随机推荐
- Python静态方法、类方法、属性方法
静态方法 使用静态方法以后,相当于把下面的函数和类的关系截断了,它的作用相当于是类下面的一个独立函数,不会自动传入参数self. class people:..... @staticmethod de ...
- from selenium.webdriver.chrome.options import Options中add_argument 常用参数表收集
chrome_options.add_argument("xxx") 序号 参数 说明 1 --allow-outdated-plugins 不停用过期的插件. 2 --allow ...
- 关于docker容器访问的主机的端口问题
docker容器需要访问主机的,不能使用127.0.0.1,127.0.0.1访问的是docker容器不是主机: docker容器创建时会分配一个主机ip,可在主机使用命令 docker inspec ...
- SpringBoot学习- 3、整合MyBatis
SpringBoot学习足迹 1.下载安装一个Mysql数据库及管理工具,同类工具很多,随便找一个都可以,我在windows下做测试项目习惯使用的是haosql 它内部集成了MySql-Front管理 ...
- jQuery图片剪裁插件Cropper.js的使用
插件下载地址及文档说明 1.引入必要的js和css核心文件 <link rel="stylesheet" href="../css/cropper.css" ...
- mybatis(三):框架结构
- 虫师自动化测试robot Framework 框架的学习2
循环的使用 1.in range和in的区别 输出结果 如果把上面的换成in range 会报错 未被定义,说明in range 后面使用的数据类型有限制,对比下,可以看出,in 可用在列表类型数据类 ...
- 随机数模块random_python
一.随机数模块random 1.常用的几个方法: import randomprint(random.random()) #(0,1)之间的随机数字,如0.6772275352932792print( ...
- error C2825: '_Iter': 当后面跟“::”时必须为类或命名空间 -- 原因可能是参数错误或者自定义函数名和库函数名冲突
今天运行程序的时候遇到了下面这个bug > B1020.cpp >e:\vs2013\vs2013_rtm_ult_chs\data\vc\include\xutility(): erro ...
- 题解【洛谷P3478】[POI2008]STA-Station
题面 设\(dp_i\)表示以\(i\)为根节点时所有节点的深度之和. 首先以 \(1\) 为根求出所有点深度之和\(dp_1\),并预处理每个点的子树大小. 设 \(v\) 是 \(u\) 的孩子, ...