2019 ICPC 南京网络赛 H-Holy Grail
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.对于自己写的模板,没有经过验证,使用起来,漏洞百出。
2.对于用别人的模板,看着难受,改起来费劲容易出错。
3.鉴于上一条,就要写第一条,所以以后改板子,写板子都要记下来,最好是每次自己写,现在有点后悔。
所以导致超时4遍,wa了4遍,所以吸取我的教训,除了非常固定的模板,其他都自己写,养成习惯,每次写都是对于这个算法思想的再认识。
这个题目的思路我是秒出的,因为打眼一看就能看出这个题目说的是带负边权的最短路问题,现在我们将范围缩小,只剩下了SPFA,Bellman-ford,和Floyd。 虽然这个题Floyd能过,但是不是这个题的正解。这个题首选的是bellman 或者SPFA,然后就是求填边之后的非负最小环问题。共跑6遍Bellman-ford
#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#include<cstdio>
#include<cstring>
#define Swap(a,b) a^=b^=a^=b
#define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define cins(s) scanf("%s",s)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
#define speed ios_base::sync_with_stdio(0)
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
#define mem(n,x) memset(n,x,sizeof(n))
#define INF 0x3f3f3f3f
#define maxn 305
#define esp 1e-9
#define mp(a,b) make_pair(a,b)
using namespace std;
typedef long long ll;
//-----------------------*******----------------------------//
const int N=1000;
int n,m;//点数,边数,编号都从0开始
long long w[N];//w[i]表示第i条边的权值(距离)
int u[N],v[N];//u[i]和v[i]分别表示第i条边的起点和终点
long long dis[N];//单源最短路径
const long long inf=(1LL<<60);
void ford(int s)
{
for(int i=0;i<=n+2;i++)
dis[i]=inf;
dis[s]=0;
for(int i=1;i<=n-1;i++)//枚举除终点外的所有点
for(int j=1;j<=m;j++)//枚举所有边
{
int x=u[j];//边j的起点
int y=v[j];//边j的终点
if(dis[x]<inf)//松弛
dis[y]=min(dis[y],dis[x]+w[j]);
}
} //就是这个板子,难受的雅痞
int main()
{
// cout<<inf;
int T;
cini(T);
while(T--)
{
cini(n);
cini(m);
for(int i=1; i<=m; i++)
{
int x,y;
long long z;
cini(x),cini(y),cinl(z);
u[i]=x;
v[i]=y;
w[i]=z;
}
for(int i=0;i<6;i++)
{
int x,y;
cini(x),cini(y);
ford(y);
long long z=dis[x];
u[++m]=x;
v[m]=y;
w[m]=-z;
printf("%lldn",-z);
}
}
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Conda | 轻松安装生信工具
- R语言绘图 | 气泡矩阵图
- 在测试自动化中使用Java枚举
- 前端|初学vue
- 使用clusterProfiler对非模式生物进行富集分析
- 微信小程序|逻辑判断
- R语言绘图 | 给气泡矩阵图上个色
- 前端|创建简单动态时钟
- R语言做几何布朗运动的模拟:复杂金融产品的几何布朗运动的模拟
- 手把手教你使用Flask搭建ES搜索引擎(实战篇)
- 【基础篇】Python+Go——带大家一起另寻途径提高计算性能
- 转录组分析 | 使用Trimmomatic过滤Fastq文件
- 转录组分析 | 使用FastQC进行数据质控
- R语言绘图 | 给箱线图加个点
- R语言绘图 | 使用pheatmap快速绘制热图