poj3159 差分约束 spfa

  1 //Accepted    2692 KB    1282 ms
  2 //差分约束 -->最短路
  3 //TLE到死,加了输入挂,手写queue
  4 #include <cstdio>
  5 #include <cstring>
  6 #include <iostream>
  7 #include <queue>
  8 #include <cmath>
  9 #include <algorithm>
 10 using namespace std;
 11 /**
 12   * This is a documentation comment block
 13   * 如果有一天你坚持不下去了,就想想你为什么走到这儿!
 14   * @authr songt
 15   */
 16 const int imax_n = 30005;
 17 const int imax_e = 150005;
 18 const int inf = 0x3f3f3f3f;
 19 struct node
 20 {
 21     int u,v,c;
 22     node(int u=0,int v=0,int c=0):u(u),v(v),c(c)
 23     {
 24
 25     }
 26 }p[imax_e];
 27 int e=0;
 28 int head[imax_n];
 29 int next[imax_e];
 30 int dis[imax_n];
 31 bool vis[imax_n];
 32 int n,m;
 33 void addEdge(int u,int v,int c)
 34 {
 35     //p[e]=node(u,v,c);
 36     p[e].u=u;
 37     p[e].v=v;
 38     p[e].c=c;
 39     next[e]=head[u];
 40     head[u]=e++;
 41 }
 42 bool relax(int u,int v,int c)
 43 {
 44     if (dis[v]>dis[u]+c)
 45     {
 46         dis[v]=dis[u]+c;
 47         return true;
 48     }
 49     return false;
 50 }
 51 void init()
 52 {
 53     memset(head,-1,(n+2)*sizeof(head[0]));
 54     memset(next,-1,(n+2)*sizeof(next[0]));
 55     e=0;
 56 }
 57 //queue<int > q;
 58 int q[imax_e];
 59 int top;
 60 void spfa(int src)
 61 {
 62     //while (!q.empty()) q.pop();
 63     //memset(vis,0,(n+2)*sizeof(vis[0]));
 64     for (int i=1;i<=n;i++)
 65     {
 66         dis[i]=inf;
 67         vis[i]=0;
 68     }
 69     dis[src]=0;
 70     //q.push(src);
 71     top=1;
 72     q[0]=src;
 73     vis[src]=true;
 74     while (top)
 75     {
 76         //int pre=q.front();
 77         //q.pop();
 78         int pre=q[--top];
 79
 80         vis[pre]=false;
 81         for (int i=head[pre];i+1;i=next[i])
 82         {
 83             if (relax(pre,p[i].v,p[i].c) && !vis[p[i].v])
 84             {
 85                 vis[p[i].v]=true;
 86                 //q.push(p[i].v);
 87                 q[top++]=p[i].v;
 88             }
 89         }
 90     }
 91 }
 92 /**
 93  * 读取一个int
 94  */
 95 inline int read_int()
 96 {
 97     int ret=0;
 98     char tmp;
 99     while(!isdigit(tmp=getchar()));
100     do{
101         ret=(ret<<3)+(ret<<1)+tmp-‘0‘;
102     }while(isdigit(tmp=getchar()));
103     return ret;
104 }
105
106 int main()
107 {
108     //while (scanf("%d%d",&n,&m)!=EOF)
109     scanf("%d%d",&n,&m);
110     {
111         init();
112         int u,c,v;
113         for (int i=0;i<m;i++)
114         {
115             u=read_int();
116             v=read_int();
117             c=read_int();
118             addEdge(u,v,c);
119         }
120         spfa(1);
121         printf("%d\n",dis[n]);
122     }
123     return 0;
124 }

时间: 2024-12-05 06:04:24

poj3159 差分约束 spfa的相关文章

POJ 3169 Layout (差分约束+SPFA)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6832   Accepted: 3292 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

ZOJ 2770 Burn the Linked Camp 差分约束+SPFA

第一道正儿八经的差分约束题 有排成一列的n个点,首先告诉你每个点的值最多是多少(最少显然要大于0),然后告诉你m段i,j,k,表示第i个点到第j个点的值的和至少有k,问你总和至少为多少. 要注意的是,告诉你的所有关系式都不要忘记建边,一开始漏了大于0的条件调半天o(╯□╰)o 不等式的形式是a-b<=c这样的= = 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <

poj3159——Candies(差分约束+SPFA堆栈)

Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse's class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and ofte

【bzoj2330】: [SCOI2011]糖果 图论-差分约束-SPFA

[bzoj2330]: [SCOI2011]糖果 恩..就是裸的差分约束.. x=1 -> (A,B,0) (B,A,0) x=2 -> (A,B,1)  [这个情况加个A==B无解的要特判] x=3 -> (B,A,0)  [恩这个是不少于一开始zz建反了] x=4 -> (B,A,1) x=5 -> (A,B,0) 然后源点到所有点建1的边[恩据说有条链所以要反着连]跑最长路就好了 1 /* http://www.cnblogs.com/karl07/ */ 2 #inc

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

Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbe

poj3169——Layout(差分约束+SPFA判断负环)

Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbe

BZOJ 2330 [SCOI2011]糖果 差分约束spfa版

题意:自行百度,(之前做过一道candy的升级版). 方法:差分约束 解析:最近在学差分约束什么的,这道是做的第一个bz上的题,感觉还是较简单的.以下我对5种操作进行描述. case 转换不等式 转换不等式2 1 A>=0+B B>=0+A 2 B>=1+A 3 A>=0+B 4 A>=1+B 5 B>=0+A 如上表按照差分约束的原理加边,然后再观察上表不等式方向->为求大边,即最长路. 这些边是不够的,所有人应最少为1糖果,即创出个源点到各点距离为1. 后记:

POJ 3159 Candies(差分约束+spfa+链式前向星)

题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A的糖果数<=C . 最后求n 比 1 最多多多少颗糖果. 解题思路:经典差分约束的题目,具体证明看这里<数与图的完美结合——浅析差分约束系统>. 不妨将糖果数当作距离,把相差的最大糖果数看成有向边AB的权值,我们得到 dis[B]-dis[A]<=w(A,B).看到这里,我们可以联想到

HDU 1384 Intervals【差分约束-SPFA】

类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b<=k2,c−a<=k3.将a,b,c转换为节点:k1,k2,k3转换为边权:减数指向被减数,形成一个有向图: 由题可得(b−a) + (c−b) <= k1+k2,c−a<=k1+k2.比较k1+k2与k3,其中较小者就是c−a的最大值.由此我们可以得知求差的最大值,即上限被约束,此时我