洛谷—— P2136 拉近距离

题目背景

我是源点,你是终点。我们之间有负权环。 ——小明

题目描述

在小明和小红的生活中,有N个关键的节点。有M个事件,记为一个三元组(Si,Ti,Wi),表示从节点Si有一个事件可以转移到Ti,事件的效果就是使他们之间的距离减少Wi。

这些节点构成了一个网络,其中节点1和N是特殊的,节点1代表小明,节点N代表小红,其他代表进展的阶段。所有事件可以自由选择是否进行,但每次只能进行当前节点邻接的。请你帮他们写一个程序,计算出他们之间可能的最短距离。

输入输出格式

输入格式:

第1行,两个正整数N,M.

之后M行,每行3个空格隔开的正整数Si,Ti,Wi。

输出格式:

一行,一个整数表示他们之间可能的最短距离。如果这个距离可以无限缩小,输出“Forever love”(不含引号)。

输入输出样例

输入样例#1:

3 3
1 2 3
2 3 -1
3 1 -10
输出样例#1:

-2

说明

对于20%数据,N<=10,M<=50。

对于50%数据,N<=300,M<=5000。

对于全部数据,N<=1000,M<=10000,|Wi|<=100,保证从节点1到N有路径。

思路:

注意在输入的时候要输入z*-1!!!!

这个题我们可以这样考虑:如果一个图存在负权环,那样这个图的最短路可以被无限更新。

所以,这个题我们就可以简单的处理成一个用spfa判断负环的问题了!

最后一个点特判!!

代码:

#include<queue>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 1002
#define maxn 9999999
using namespace std;
int read()
{
    ,f=;
    char ch=getchar();
    ')
    {
        ;
        ch=getchar();
    }
    ')
    {
        x=x*+ch-';
        ch=getchar();
    }
    return x*f;
}
struct Edge
{
    int to,ds,next;
}edge[N*N];
int n,m,x,y,z,head[N],tot,sum[N];
long long dis[N];
bool vis[N];
int add(int from,int to,int dis)
{
    tot++;
    edge[tot].ds=dis;
    edge[tot].to=to;
    edge[tot].next=head[from];
    head[from]=tot;
}
int spfa1(int s)
{
    memset(vis,false,sizeof(vis));
    memset(dis,0x3f,sizeof(dis));
    queue<int>q;
    dis[s]=,vis[s]=true;
    q.push(s);
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int i=head[x];i;i=edge[i].next)
        {
            if(dis[x]+edge[i].ds<dis[edge[i].to])
            {
                dis[edge[i].to]=edge[i].ds+dis[x];
                q.push(edge[i].to);
                sum[edge[i].to]++;
                ;
            }
        }
        //vis[x]=false;
    }
    ;
}
int spfa2(int s)
{
    memset(vis,false,sizeof(vis));
    memset(dis,0x3f,sizeof(dis));
    queue<int>q;
    dis[s]=,vis[s]=true;
    q.push(s);
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int i=head[x];i;i=edge[i].next)
        {
            if(dis[x]+edge[i].ds<dis[edge[i].to])
            {
                dis[edge[i].to]=edge[i].ds+dis[x];
                if(!vis[edge[i].to])
                {
                    vis[edge[i].to]=true;
                    q.push(edge[i].to);
                }
            }
        }
        vis[x]=false;
    }
}
int main()
{
    n=read(),m=read();
    ;i<=m;i++)
    {
        x=read(),y=read(),z=read();
        add(x,y,z*-);
    }
    )
    {
      printf("-40");
      ;
    }
    ;i<=n;i++)
    {
        )
        {
            int ans=spfa1(i);
            )
            {
                printf("Forever love");
                ;
             }
        }
    }
    spfa2();
    printf("%d",dis[n]);
    ;
}

最新文章

  1. 本地推送UILocalNotification
  2. 背水一战 Windows 10 (10) - 资源: StaticResource, ThemeResource
  3. linux下安装rzsz
  4. PDF 补丁丁 0.4.2.1023 测试版发布:新增旋转页面功能
  5. linux 多线程基础3
  6. HDU 3660 Alice and Bob&#39;s Trip
  7. Lintcode--005(最长公共子序列)
  8. IBM即将倒闭,微软也从崩溃18个月
  9. 物流进程html+css页面
  10. body.clientHeight与documentElement.clientHeight
  11. 安卓自定义控件(二)BitmapShader、ShapeDrawable、Shape
  12. 【javaweb学习笔记】WEB02_HTML&amp;CSS
  13. 重新安装了环境报错{&quot;error&quot;:&quot;could not find driver&quot;}
  14. WebApi返回类型设置为json的三种方法
  15. Caused by: java.sql.SQLException: Value &#39;0000-00-00 00:00:00&#39; can not be represented as java.sql.Timestamp
  16. Dart 语言简易教程系列
  17. 简易版AC自动机
  18. imei和imsi
  19. pixel像素基础
  20. unity 加载读取外部XML

热门文章

  1. jQuery绑定动态元素的点击事件无效
  2. Bootstrap教程简介
  3. 寄存器变量 extern 外部变量 外部函数
  4. Ukulele 原来你也在这里
  5. ios 自定义RadioButton
  6. CSS 不换行 white-space 属性详解
  7. Linux-nginx服务(三)
  8. 【linux】【git】安装/升级Git 1.9.4
  9. 【php】 php的注释问题,单行注释和多行注释与php结束符的关系
  10. 【HIHOCODER 1067】最近公共祖先&#183;二(LCA)