//差分约束  >=求最长路径 <=求最短路径   结果都一样
//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. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(二)

    这篇文章的理解,需要一些专业知识了. 我们可以创建模拟自己的外设吗? 我们已经知道什么是qemu了,我们可以通过qmeu的提供的外设,DIY一个计算机了. 但是我们可能还不满足,我们可以自己制造一个外 ...

  2. 使用sphinx索引mysql数据

    数据库表如下 mysql> select * from tb_account; +----+-------+------+ | id | name | age | +----+-------+- ...

  3. iOS开发基本须要

        iOS开发基本须要 1 准备好对应的硬件和软件配置------基于Intel处理器的Mac操作系统 2 具有C++,Java或其它面向对象编程语言的开发经验 3 最新的iphone SDK的下 ...

  4. android 三级菜单 BaseExpandableListAdapter

    在网上搜了非常长时间.没有找到合适的Android三级菜单.所以就自己动手写了一个,主要使用了BaseExpandableList来实现,通过三个布局文件来完毕相应的菜单项,详细实现请參照下图. wa ...

  5. ACdream群赛1112(Alice and Bob)

    题意:http://acdream.info/problem?pid=1112 Problem Description Here  is Alice and Bob again ! Alice and ...

  6. 统计分析SQL Server Profiler 跟踪的SQL

    --跟踪文件读入到表中分析 SELECT * INTO ZGSJY FROM fn_trace_gettable('E:\wxxcdbprofiler.trc', default); --某时间内,最 ...

  7. shell命令基础

    1.修改密码 使用 passwd 命令修改密码. 该命令如果在 root 用户下执行,则修改的是 root 用户的密码. 2.获取帮助 使用 ls --help 命令获取帮助. [zhanghuiju ...

  8. Linux命令之修改主机名

    ubuntu永久修改主机名 1.查看主机名 在Ubuntu系统中,快速查看主机名有多种方法: 其一,打开一个GNOME终端窗口,在命令提示符中可以看到主机名,主机名通常位于“@”符号后: 其二,在终端 ...

  9. Hibernate知识总结(一)——Hibernate原理概述

    一.Hibernate是什么: 它是一个持久化框架,它对JDBC进行了轻量级的封装,简化对数据库的操作,提高开发效率.和另一个持久化框架MyBatis一样,他们操作数据库都是通过一个session对象 ...

  10. Kafka主要参数详解

    原文档地址:http://kafka.apache.org/documentation.html  https://kafka.apache.org/08/documentation#configur ...