//Accepted    2692 KB    1282 ms
 //差分约束 -->最短路
 //TLE到死,加了输入挂,手写queue
 #include <cstdio>
 #include <cstring>
 #include <iostream>
 #include <queue>
 #include <cmath>
 #include <algorithm>
 using namespace std;
 /**
   * This is a documentation comment block
   * 如果有一天你坚持不下去了,就想想你为什么走到这儿!
   * @authr songt
   */
 ;
 ;
 const int inf = 0x3f3f3f3f;
 struct node
 {
     int u,v,c;
     node(,,):u(u),v(v),c(c)
     {

     }
 }p[imax_e];
 ;
 int head[imax_n];
 int next[imax_e];
 int dis[imax_n];
 bool vis[imax_n];
 int n,m;
 void addEdge(int u,int v,int c)
 {
     //p[e]=node(u,v,c);
     p[e].u=u;
     p[e].v=v;
     p[e].c=c;
     next[e]=head[u];
     head[u]=e++;
 }
 bool relax(int u,int v,int c)
 {
     if (dis[v]>dis[u]+c)
     {
         dis[v]=dis[u]+c;
         return true;
     }
     return false;
 }
 void init()
 {
     memset(head,-,(n+)*]));
     memset(next,-,(n+)*]));
     e=;
 }
 //queue<int > q;
 int q[imax_e];
 int top;
 void spfa(int src)
 {
     //while (!q.empty()) q.pop();
     //memset(vis,0,(n+2)*sizeof(vis[0]));
     ;i<=n;i++)
     {
         dis[i]=inf;
         vis[i]=;
     }
     dis[src]=;
     //q.push(src);
     top=;
     q[]=src;
     vis[src]=true;
     while (top)
     {
         //int pre=q.front();
         //q.pop();
         int pre=q[--top];

         vis[pre]=false;
         ;i=next[i])
         {
             if (relax(pre,p[i].v,p[i].c) && !vis[p[i].v])
             {
                 vis[p[i].v]=true;
                 //q.push(p[i].v);
                 q[top++]=p[i].v;
             }
         }
     }
 }
 /**
  * 读取一个int
  */
 inline int read_int()
 {
     ;
     char tmp;
     while(!isdigit(tmp=getchar()));
     do{
         ret=(ret<<)+(ret<<)+tmp-';
     }while(isdigit(tmp=getchar()));
     return ret;
 }

 int main()
 {
     //while (scanf("%d%d",&n,&m)!=EOF)
     scanf("%d%d",&n,&m);
     {
         init();
         int u,c,v;
         ;i<m;i++)
         {
             u=read_int();
             v=read_int();
             c=read_int();
             addEdge(u,v,c);
         }
         spfa();
         printf("%d\n",dis[n]);
     }
     ;
 }

poj3159 差分约束 spfa的更多相关文章

  1. 【poj3169】【差分约束+spfa】

    题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...

  2. O - Layout(差分约束 + spfa)

    O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...

  3. POJ-3159.Candies.(差分约束 + Spfa)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 40407   Accepted: 11367 Descri ...

  4. 【BZOJ】2330: [SCOI2011]糖果(差分约束+spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束运用了最短路中的三角形不等式,即d[v]<=d[u]+w(u, v),当然,最长 ...

  5. (简单) POJ 3169 Layout,差分约束+SPFA。

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  6. poj Layout 差分约束+SPFA

    题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...

  7. BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)

    BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...

  8. 图论分支-差分约束-SPFA系统

    据说差分约束有很多种,但是我学过的只有SPFA求差分: 我们知道,例如 A-B<=C,那么这就是一个差分约束. 比如说,著名的三角形差分约束,这个大家都是知道的,什么两边之差小于第三边啦,等等等 ...

  9. HDU 1384 Intervals【差分约束-SPFA】

    类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b&l ...

随机推荐

  1. 红字差评系列3.abcd

    [题目分析] 首先,这个e[i]是在a[i]~b[i]的,而且要{c[i]*e[i]}为0,{d[i]*e[i]}最大. 我们把a[i]~b[i]这个区间向左平移a[i]个单位,于是这个区间就变成了0 ...

  2. centos6.6编译安装lnmp系列之PHP

    简介: 环境:虚拟机+centos6.6 在百度云盘里面我分享的安装软件包,包含LNMP系列软件. http://pan.baidu.com/s/1gdvnimv 1.安装前配置: 关闭selinux ...

  3. lua中string.find()函数作用于汉字字符串

    lua中有这样一个库函数,string,find(),作用是在一个字符串中找到目标字符串的起始和结束位置(从1开始计数) 如:a,b=string.find("hello world&quo ...

  4. shell zsh

    之前用fish安装homebrew成功了 但是忘记怎么安装的了 以后要纪录下来了 设置zsh为默认的 shell https://github.com/robbyrussell/oh-my-zsh/w ...

  5. redis 数据导出

    一.导出所有的keys echo "keys 201*" |./redis-cli -h localhost -p 6379 -a password >> 1.txt ...

  6. 【转】Vim十大必备插件

    [转]Vim十大必备插件 转自:http://my.oschina.net/zhoukuo/blog/336315 Taglist taglist是一个用于显示定位程序中各种符号的插件,例如宏定义.变 ...

  7. java 获取项目绝对路径

    String classPath = Thread.currentThread().getContextClassLoader().getResource("").getPath( ...

  8. Clojure学习笔记(一)——介绍、安装和语法

    什么是Clojure Clojure是一种动态的.强类型的.寄居在JVM上的语言. Clojure的特性: 函数式编程基础,包括一套性能可以和典型可变数据结构媲美的持久性数据结构 由JVM提供的成熟的 ...

  9. ring0

    Intel的x86处理器是通过Ring级别来进行访问控制的,级别共分4层,RING0,RING1,RING2,RING3.Windows只使用其中的两个级别RING0和RING3. RING0层拥有最 ...

  10. android 发短信

    SmsManager smsManager = SmsManager.getDefault(); List<String> divideContents = smsManager.divi ...