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 ...
随机推荐
- (CPSCA's)CPOJC+VIJOS
Coding Plus System Core Association 建立的Coding Plus Online Judge China 在Vijos上初步落脚,让我们拭目以待,等待暑假期间ACM1 ...
- new SparkContext()发生错误java.lang.NoSuchMethodError: scala.Predef
参考:https://blog.csdn.net/weixin_40137479/article/details/80320324 new SparkContext(conf)发生错误: Except ...
- 3.Docker 操作镜像
获取镜像 之前提到过,Docker Hub 上有大量的高质量的镜像可以用,这里我们就说一下怎么获取这些镜像. 从 Docker 镜像仓库获取镜像的命令是 docker pull.其命令格式为: doc ...
- Attention machenism
from attention mechanism Attention is one component of a network’s architecture, and is in charge of ...
- (转)HashMap和HashTable源码
转自: http://www.cnblogs.com/ITtangtang/p/3948406.html http://frankfan915.iteye.com/blog/1152091 一.Has ...
- 安装postman时遇到“无法定位程序输入点 SetDefaultDllDirectories于动态链接库KERNEL32.dll 上.”的问题
安装postman时遇到“无法定位程序输入点 SetDefaultDllDirectories于动态链接库KERNEL32.dll 上.”的问题 解决办法: 1.安装系统更新补丁KB2533623,下 ...
- Node.js、npm和webpack的安装
1. 前往Node.js官网下载安装程序 2. 一路点击下一步即可 3. 测试是否安装成功 4. 配置npm在安装全局模块时的路径和缓存cache的路径 因为在执行例如npm install webp ...
- c++踩坑大法好 typedef和模板
1,typedef字面意思,自定义一种数据类型 语法:typedef 类型名称 类型标识符; 基本用法: 1) 为基本数据类型定义新的类型名. 2) 为自定义数据类型(结构体.公用体和枚举类型)定义简 ...
- 使用shader,矩阵旋转实现图片的旋转动画
常用于loading动画之类的 具体的实现代码: fixed4 frag (v2f i) : SV_Target { //1.先将uv平移到原点(让图片中心与原点重合) float2 pianyi=( ...
- 2019-08-15 纪中NOIP模拟B组
T1 [JZOJ3455] 库特的向量 题目描述 从前在一个美好的校园里,有一只(棵)可爱的弯枝理树.她内敛而羞涩,一副弱气的样子让人一看就想好好疼爱她.仅仅在她身边,就有许多女孩子想和她BH,比如铃 ...