//差分约束  >=求最长路径 <=求最短路径   结果都一样
//spfa
#include<stdio.h>
#include<string.h>
#include<limits.h>
#include<queue>
using namespace std;
#define N 1010
#define M 1010*1010//注意边和点集的数组大小
struct edge
{
int to,value,next;
};
struct edge edges[M];
int heads[N],len=;
int dis[N];
int addedge(int u,int v,int w)
{
edges[len].to=v,edges[len].value=w,edges[len].next=heads[u];
heads[u]=len++;
return ;
}
int n,m; int spfa(int v)
{
queue<int> q;
int inqueue[N];
memset(inqueue,,sizeof(inqueue)),inqueue[v]=;
q.push(v);
for(int i=;i<=n;i++) dis[i]=INT_MIN;
dis[v]=;
int times[N];
memset(times,,sizeof(times)),times[v]=;
int temp=;
while(!q.empty()){
int x=q.front();
q.pop();
inqueue[x]=; for(int i=heads[x];i!=-;i=edges[i].next){
int to=edges[i].to,value=edges[i].value;
if(value+dis[x]>dis[to]){
dis[to]=value+dis[x]; if(!inqueue[to]){ //注意已经在队列里面的不用再加入队列
inqueue[to]=,q.push(to);
times[to]++;
if(times[x]>n){
return -;//返回值有可能是0 不能将0作为区别的标记 } }
}
}
}
return dis[n];
}
int main(void)
{ int i,num;
while(scanf("%d%d",&n,&m)!=EOF){
len=;
memset(heads,-,sizeof(heads));
for(i=;i<=n;i++){
scanf("%d",&num);
addedge(i,i-,-num);
addedge(,i,);
}
for(i=;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u-,v,w);
} int h=spfa();
if(h!=-) printf("%d\n",h);
else printf("Bad Estimations\n"); }
return ;
}
//bellman
#include <iostream>
#include <stdio.h>
#include <string.h> using namespace std;
#define INF 2000000000
int u[12005],v[12005],w[12005],n,m,total,d[1005];
void Edge(int s,int e,int val){
u[total]=s;
v[total]=e;
w[total++]=val;
}
int Bellman(){
int i,e;
for(i=1;i<=n;i++) d[i]=-INF;
d[0]=0;
for(i=1;i<=n;i++){
for(e=0;e<total;e++){
if(d[v[e]]<d[u[e]]+w[e])
d[v[e]]=d[u[e]]+w[e];
}
}
int ans=d[n];
for(e=0;e<total;e++)
if(d[v[e]]<d[u[e]]+w[e]) ans=-1;
return ans;
}
int main(){
int a,b,c,cnt,i;
while(scanf("%d%d",&n,&m)!=EOF){
total=0;
for(i=1;i<=n;i++){
scanf("%d",&c);
Edge(0,i,0);
Edge(i,i-1,-c);
}
for(i=0;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
Edge(a-1,b,c);
} cnt=Bellman();
for(i=0;i<n;i++) printf("%d ",d[i]);
puts("");
if(cnt==-1) cout<<"Bad Estimations\n";
else cout<<cnt<<"\n";
}
return 0;
}

  

 

ZOJ 2770 Burn the Linked Camp(spfa&&bellman)的更多相关文章

  1. ZOJ 2770 Burn the Linked Camp 差分约束

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=2770 Burn the Linked Camp Time Limi ...

  2. ZOJ 2770 Burn the Linked Camp 差分约束 ZOJ排名第一~

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1770 题目大意: 陆逊为了火烧连营七百里,派出了间谍刺探敌情,得之刘备的军营以 ...

  3. zoj 2770 Burn the Linked Camp

    今天刚刚学差分约束系统.利用最短路求解不等式.世界真的好奇妙!感觉不等式漏下几个会导致WA!! #include<cstdio> #include<cstring> #incl ...

  4. zoj 2770 Burn the Linked Camp (差分约束系统)

    // 差分约束系统// 火烧连营 // n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i- ...

  5. Burn the Linked Camp(bellman 差分约束系统)

    Burn the Linked Camp Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that, in the ...

  6. zoj Burn the Linked Camp (查分约束)

    Burn the Linked Camp Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that, in the ...

  7. ZOJ2770 Burn the Linked Camp(差分约束系统)

    区间和一定要联系到前缀和. 这题,把前缀和看作点,从s0到sn: 对于每一个营地i的容量capi,有这么个关系si-si-1<=capi: 对于每一个区间的评估i,j,k,有sj-si-1> ...

  8. zoj2770 Burn the Linked Camp --- 差分约束

    有n个营地,每一个营地至多容纳Ci人.给出m个条件:第i到第j个营地之间至少有k人. 问n个营地总共至少有多少人. 此题显然差分约束.要求最小值.则建立x-y>=z方程组,建图求最长路. 用d[ ...

  9. ZOJ 2770 差分约束+SPFA

    Burn the Linked Camp Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that, in the ...

随机推荐

  1. JDBC笔记

    简介 JDBC是Java规定的访问数据库的API,目前主流的数据库都支持JDBC.使用JDBC访问不同的数据库时需要安装不同的驱动. JDBC定义了数据库的链接,SQL语句的执行以及查询结果集的遍历等 ...

  2. poj2636---Electrical Outlets(插线板)

    #include <stdio.h> #include <stdlib.h> int main() { int n,nc,i; scanf("%d",&am ...

  3. jquery绑定事件,解绑事件

    unbind([type],[data]) 是 bind()的反向操作,从每一个匹配的元素中删除绑定的事件.如果没有参数,则删除所有绑定的事件.你可以将你用bind()注册的自定义事件取消绑定.如果提 ...

  4. 【和我一起学习Unity3D】Unity3D的坐标控制

    坐标这个东西,在Unity3D里面是分为几个类的,各自是Vector2,Vector3.Vector4:含义各自是:二维坐标系,三维坐标系,四维坐标系.一般做游戏呢,用到的最多的就是Vector3了. ...

  5. Python文件或目录操作的常用函数

    ◆ os.listdir(path) Return a list containing the names of the entries in the directory given by path. ...

  6. ThreadPool.QueueUserWorkItem的性能问题

    在WEB开发中,为了降低页面等待时间提高用户体验,我们往往会把一些浪费时间的操作放到新线程中在后台执行. 简单的实现代码就是: //代码一 new Thread(()=>{ //do somet ...

  7. 高效的SQLSERVER分页查询(推荐)

    Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询 ...

  8. AIDL 发生异常的原因 Android java.lang.SecurityException: Binder invocation to an incorrect interface

    我建立了两个project.一个是activity 的 ,一个是service 的. 在进行两个project通信时,应该有以下几点注意: 1.在activity project中引入service ...

  9. TASKKILL命令使用方法

    TASKKILL [/S system [/U username [/P [password]]]]          { [/FI filter] [/PID processid | /IM ima ...

  10. 【转】在SQL Server 2008中SA密码丢失了怎么办?

    sql server 2008的sa用户莫名其妙就登陆不进去了.提示如下: 以上提示就表明是密码错误,但密码我可是记得牢牢的,也许是系统被黑的原因吧.一直以来我的Windows身份验证就用不起,以下方 ...