给定一张边带权的有向图。从节点\(1\)出发,每经过一条边一次,得分加上这条边的边权。(可以多次经过,多次累加

必须在点\(n\)结束游戏

判断是否能使得分无限大,如果否,求最大得分。

sol

题目所给的边权可以为负,\(dfs\)判环+\(DAG\)上\(DP\)最长路是补锌的(?

然后很容易想到边权取相反数,跑一遍\(SPFA\)判负环+求最短路。哇,sb题,秒了

然后喜提WA QAQ

被坑了————————————————

\(SPFA\)不可行的原因是,题目要求路径从节点\(1\)到节点\(n\)。判出负环就输出\(inf\)会少考虑一种情况,那就是负环并不在\(1\)到\(n\)的路径上。

所以我们用\(BellmanFord\)

先进行\(n-1\)轮松弛,记录此时的\(dis[n]\)

再进行\(n-1\)轮松弛()新的\(dis[n]\)发生了变动,当且仅当在\(1\)到\(n\)的路径上存在负环。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int n,m;
#define MAXN (2007)
#define MAXM (4014)
struct qwq
{
int nex,to,w;
}e[MAXM];
int h[MAXN],tot=0;
inline void add(int x,int y,int z)
{
e[++tot].to=y;
e[tot].w=z;
e[tot].nex=h[x];
h[x]=tot;
}
long long dis[MAXN];
int coun[MAXN];
bool vis[MAXN];
#include <queue>
#define inf (long long)((long long)(1e16)+233)
queue<int> q;
inline void INIT() { for (int i=1;i<=n;i++) dis[i]=inf; }
inline long long spfa()//关于SPFA,_____________
{
INIT();
q.push(1);
vis[1]=true;
dis[1]=0;
int x;
while (!q.empty())
{
x=q.front();
vis[x]=false;
q.pop();
for (int i=h[x],y;i;i=e[i].nex)
{
y=e[i].to; // printf("x:%d dis:%lld y:%d\n",x,dis[x],y);
if (dis[y]>dis[x]+e[i].w)
{
dis[y]=dis[x]+e[i].w;
if (!vis[y])
{
coun[y]=coun[x]+1;
if (coun[y]>=n) return (inf<<1);
vis[y]=true;
q.push(y);
}
}
}
}
return dis[n];
}
inline long long BF()
{
INIT();
dis[1]=0;
for (int k=1;k<n;k++)
for (int x=1;x<=n;x++)
for (int i=h[x],y;i;i=e[i].nex)
{
y=e[i].to; if (dis[y]>dis[x]+e[i].w) dis[y]=dis[x]+e[i].w;
}
long long ans=dis[n];
for (int k=1;k<n;k++)
for (int x=1;x<=n;x++)
for (int i=h[x],y;i;i=e[i].nex)
{
y=e[i].to;
if (dis[y]>dis[x]+e[i].w) dis[y]=dis[x]+e[i].w;
}
if (ans!=dis[n]) return inf<<1;
else return ans;
}
signed main()
{
scanf("%d%d",&n,&m);
for (int i=1,x,y,z;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,-z);
}
long long ans=BF();
if (ans==inf<<1) puts("inf");
else printf("%lld\n",-ans);
return 0;
}

AtCoder Beginner Contest 061 - D Score Attack的更多相关文章

  1. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  2. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  3. AtCoder Beginner Contest 076

    A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...

  4. AtCoder Beginner Contest 068 ABCD题

    A - ABCxxx Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement This contes ...

  5. AtCoder Regular Contest 061

    AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...

  6. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  7. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  8. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  9. AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】

    AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...

  10. AtCoder Beginner Contest 064 D - Insertion

    AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...

随机推荐

  1. obs问题记录

    1.安装obs后,可能无法使用,需要下载dll文件,我的阿里云盘中有: 2.录制时电脑没电会报错'编码器错误': 3.插上声卡后播放视频想听到声音需要在声卡上插耳机听: 4.

  2. [CQOI2014]通配符匹配 题解

    第一眼:什么鬼东西ヾ(。`Д´。) 第二眼:显然,这道题要分段处理 类似[TJOI2018]碱基序列\ (建议做一做也是Hash+DP)\ 那你怎么第一眼没看出来 Hash处理+DP==AC 直接上代 ...

  3. 说一说webpack的配置文件

    可能有一些友友会奇怪为什么在写webpack的配置文件的时候,我们只能用commonJS导出. 原因:在打包的时候需要运行webpack的配置文件里面的代码,而webpack在打包运行的环境在node ...

  4. docker 配置 anaconda + jupyter notebook 纪要

    首先,pull官方的镜像,名字叫:continuumio/anaconda3 首先的首先,如果默认还是需要sudo运行docker命令,可以考虑把当前用户加入到docker组(usermod -aG ...

  5. 对Java要学东西的认识 (一点点javascript见解)

    JavaScript是一种脚本语言,它采用小程序段的方式实现编程.像其它脚本语言一样,JavaScript同样已是一种解释性语言,它提供了一个易的开发过程.它的基本结构形式与C.C++.VB十分类似. ...

  6. django_ORM基础字段和选项

    一.修改ORM 1.任何关于表结构的修改,务必在对应模型类上修改. 2.在上一篇博文中,创建了bookstore_book表,如果需要再添加一个名为info(varchar 100)的字段,则需要如下 ...

  7. Windows 解决teamview远程必须mstsc连接

    真实原因是你的TeamViewer一直在用远程桌面的ID进行登录,所以一旦远程桌面断开,TeamViewer就无法连接了.因此我们只需要切换为服务器的TeamViewer ID即可,服务器的TeamV ...

  8. 归纳了一下AD的快捷键

    1:shift+s 键 切换单层显示 2:q     英寸和毫米 尺寸切换3:D+R进入布线规则设置.其中 Clearance 是设置最小安全线间距,覆铜时候间距的.比较常用4:CTRL+鼠标单击某个 ...

  9. python语言linux操作系统oracle环境安装

    金句:如果没把握,最好先Google一下. 1.严格按照 https://oracle.github.io/odpi/doc/installation.html#linux 教程一步步做 包括下载的软 ...

  10. MAC完整的地址

    作者:匿名用户 链接:https://www.zhihu.com/question/22883229/answer/71280098 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转 ...