http://acm.hdu.edu.cn/showproblem.php?pid=3592

题意:有N个人按照1-N 的顺序排成一排,给你X个关于他们位置的关系,如:a, b ,c,则说明编号为a的人在标号为b 的人的前面(话说我实在没在原文中看到a必须在b前面,除非这句 Assume that there are N (2 <= N <= 1,000) people numbered 1..N who are standing in the same order as they are numbered. It is possible that two or more person line up at exactly the same location in the condition that those visit it in a group.应该是我英语不好的缘故),且两人最多相隔c距离,再给你Y给位置关系,给出的是a和b两个人至少相距c,问1号人和N号人最远相距多少。如果不存在这样的排序,则输出-1 ,如果1和N可以相距任意的距离,则输出-2, 否则输出最长的距离。

分析:在纸上写出式子就看出来了。

a[1]-a[3]<=8;                       >>>a[1]-a[3]<=8

a[2]-a[4]<=15;           转化为  >>>a[2]-a[4]<=15

a[2]-a[3]>=4                        >>>a[3]-a[2]<=-4

这样就可以建图了。

对于x

Createmap(a,b,w);

对于y

CreateMap(b,a,-w);

还有一个要注意的是:输出有3种情况。具体看代码。

View Code

//// I'm lanjiangzhou
////C
//#include <stdio.h>
//#include <stdlib.h>
// I'm lanjiangzhou
//C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
//C++
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cctype>
#include <stack>
#include <string>
#include <list>
#include <queue>
#include <map>
#include <vector>
#include <deque>
#include <set>
using namespace std;//*************************OUTPUT*************************
#ifdef WIN32
#define INT64 "%I64d"
#define UINT64 "%I64u"
#else
#define INT64 "%lld"
#define UINT64 "%llu"
#endif//**************************CONSTANT***********************
#define INF 0x3f3f3f3f// aply for the memory of the stack
//#pragma comment (linker, "/STACK:1024000000,1024000000")
//end
const int maxn =1010;
int head[maxn];
int vis[maxn];
int _count[maxn];//用来记录同一个点入队列的次数
int n,cnt;struct point{int v,next;//终点,指向下一条边int w;//权值
}edge[maxn*maxn];int dis[maxn];void CreateMap(int u,int v,int w){edge[cnt].v=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;
}int spfa(){memset(vis,0,sizeof(vis));memset(_count,0,sizeof(_count));for(int i=1;i<=n;i++){dis[i]=INF;}dis[1]=0;vis[1]=1;_count[1]++;queue<int> Q;Q.push(1);while(!Q.empty()){int tmp=Q.front();Q.pop();vis[tmp]=0;for(int i=head[tmp];i!=-1;i=edge[i].next){if(dis[tmp]+edge[i].w<dis[edge[i].v]){dis[edge[i].v]=dis[tmp]+edge[i].w;if(!vis[edge[i].v]){Q.push(edge[i].v);vis[edge[i].v]=1;if((++_count[edge[i].v])>n)return -1;//存在负权回路
                }}}}if(dis[n]==INF) return -2;return dis[n];//return 1;
}int main(){//int temp;//double l,u;int t;int x,y;int u,v,w;scanf("%d",&t);while(t--){memset(head,-1,sizeof(head));scanf("%d%d%d",&n,&x,&y);cnt=1;//int temp;for(int i=1;i<=x;i++){scanf("%d%d%d",&u,&v,&w);CreateMap(u,v,w);// for(int j=1;j<=m;j++){//scanf("%d",&temp);// CreateMap(i,j+n,-log(l*1.0/temp));// CreateMap(j+n,i,log(u*1.0/temp));// }
        }for(int i=1;i<=y;i++){scanf("%d%d%d",&u,&v,&w);CreateMap(v,u,-w);}printf("%d\n",spfa());//int flag=spfa();//if(flag) printf("%d\n",dis[n]);//else  if(dis[n]==INF) printf("-2\n");//else  if(!flag)   printf("-1\n");
    }return 0;
}

还有一个小点,我用bellman_ford()一直有问题。再试试吧。加油小菜鸟州。

下面是bellman_ford算法做的还没做出来。

View Code

// I'm lanjiangzhou
//C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
//C++
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cctype>
#include <stack>
#include <string>
#include <list>
#include <queue>
#include <map>
#include <vector>
#include <deque>
#include <set>
using namespace std;//*************************OUTPUT*************************
#ifdef WIN32
#define INT64 "%I64d"
#define UINT64 "%I64u"
#else
#define INT64 "%lld"
#define UINT64 "%llu"
#endif//**************************CONSTANT***********************
#define INF 0x3f3f3f3f//aply for the memory of the stack
//#pragma comment (linker, "/STACK:1024000000,1024000000")
//endconst int nmax = 1000+10;
const int emax = 23000+100;
int n;//一共有几个大营
int m;//已知从第i个大营到第j个大营至少有几个士兵,这些信息有m条
int c[nmax];//第i个大营最多有c[i]个士兵
int d[nmax];//d[0]=0;  d[1]=c[1];  d[2]=c[1]+c[2]....即d[i]为前i个大营容量总和
int ei;//边的序号
int dis[nmax];//从源点到个顶点最短路径长度(注意:源点为sn而不是s0)struct eg{int u,v,w;//边:起点,终点,权值
}edges[emax];void init(){ei=0;//除源点sn外,其他顶点的最短距离初始为inf// 则bellman_ford算法的第一重要循环n-1次for(int i=1;i<=n;i++) {dis[i]=INF;}dis[1]=0;dis[n]=0; //一下bellman_ford算法是以sn为源点的,所以dis[n]=0;
}bool bellman_ford(){//init();//bellman_ford()//算法的第一重循环要执行n-1次,n是网络中顶点的个数//本题中顶点个数n+1;for(int i=1;i<=n;i++){//   假设第k条边的起点是u,终点是v,以下循环考虑第k条边是否会使得源点v0到v的最短距离缩短,//  即判断dis[edges[k].u]+edges[k].w<dis[edges[k].v]是否成立for(int k=1;k<=ei;k++){int t=dis[edges[k].u]+edges[k].w;if(dis[edges[k].u]!=INF&&t<dis[edges[k].v]){dis[edges[k].v]=t;// printf("t=%d\n",t);
            }}}//以下是检查,若还有更新则说明存在无限循环的负值回路for(int k=1;k<=ei;k++){if(dis[edges[k].u]!=INF&&dis[edges[k].u]+edges[k].w<dis[edges[k].v]){return -1;}}if(dis[n]==INF) return -2;return dis[n];//return true;
}int main(){int t;scanf("%d",&t);while(t--){//init();int x,y;scanf("%d%d%d",&n,&x,&y);//init();int u,v,w;for(int i=1;i<=x;i++){scanf("%d%d%d",&u,&v,&w);edges[ei].u=u;edges[ei].v=v;edges[ei].w=w;ei++;}for(int j=1;j<=y;j++){scanf("%d%d%d",&u,&v,&w);edges[ei].u=v;edges[ei].v=u;edges[ei].w=-w;//构造边<v,u-1>  权值为-w;ei++;}init();printf("%d\n",bellman_ford());// int ans=bellman_ford();//if(ans) printf("-1\n");//else printf("%d\n",dis[n]);
    }return 0;
}

转载于:https://www.cnblogs.com/lanjiangzhou/archive/2013/04/07/3005973.html

hdu 3592 差分约束相关推荐

  1. hdu 1534(差分约束)

    题意: 安排计划,有4种约束方式,给出你这些时间的n个约束.. 如果计划是可行的,求出每一件事发生的最早时间..否则输出"impossible".. ①. FAF a b a要在b ...

  2. hdu 4598 差分约束

    思路:首先就是判断是否有奇环,若存在奇环,则输出No. 然后用差分约束找是否符合条件. 对于e(i,j)属于E,并且假设顶点v[i]为正数,那么v[i]-v[j]>=T--->v[j]-v ...

  3. 差分约束 1:pku 1201 Intervals 2:pku 1364 King 3:hdu 1534

    一个很好的差分约束总结:http://972169909-qq-com.iteye.com/blog/1185527 第一:  感觉难点在于建图  第二:  ①:对于差分不等式,a - b <= ...

  4. 2017 CCPC final HDU - 6252 Subway Chasing (差分约束)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6252 题目大意:有n个车站,两个人轮流从第一个车站出发,第一个人先出发x分钟,第二个人再出发.接下来给 ...

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

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

  6. 【HDU - 3440】House Man(差分约束)

    题干: In Fuzhou, there is a crazy super man. He can't fly, but he could jump from housetop to housetop ...

  7. HDU 3440 House Man (差分约束)

    题意:有N个在一条直线上的房子, 每个房子有着不同的高度,超人可以将这些房子左右移动,但不能改变房子之间的相对位置.超人要从最矮的房子跳到刚好比他高的房子上面, 且每次跳的房子都要比当前房子要高,他要 ...

  8. HDU 4598 Difference 差分约束 + 判奇圈

    题意:给你一个无向图 这个图是difference的如果存在一个正实数T使得图中所有的点的绝对值|ai|<T 并且点i j构成一条边当且仅当|ai-aj|>=T 问你是否存在一个这样的图 ...

  9. HDU - 6252 Subway Chasing (差分约束)

    题目链接 题意:现在有两个人,其中一个人比另外一个人先走x分钟,然后两个人在途中不停的聊天,要是两个人报的点一样说明该人正好在那个点,要是不一样,则在两个点中间,现在要让你求两个站之间的长度(只要满足 ...

最新文章

  1. [SOJ] 简单哈希
  2. Spring Boot 2.x基础教程:实现文件上传
  3. 为什么美国财媒IBD力推网易为第1强股?
  4. 商业软件划分的网格导入OpenFOAM问题总结【终极】
  5. 大地SEO教程学习笔记之八:
  6. linux windows文件 编码_Mac, Windows和Linux电脑之间如何快速传输文件
  7. datagrip将一个数据库中的数据_跨平台数据库管理神器DataGrip,用上就爱不释手...
  8. 软件工程小组第九次会议记录
  9. 计算机的运算符号,运算符号包括哪些
  10. HTML5制作诗歌锦集,轻叩诗歌的大门作文锦集6篇
  11. Wemos D1 Mini / nodeMcu / esp8266 + GUIslice库 驱动ST7789 TFT显示屏
  12. MacBook雷电3接口失灵不可用
  13. 电脑计算机D盘红格式化不了,电脑D盘无法格式化提示Windows无法格式该驱动器的解决办法...
  14. Ubuntu Linux出现IP inet6 addr: fe80::fe0:9b43:8a0e:2463/64的解决办法
  15. 模电(二)半导体二极管
  16. VGG16-好莱坞明星识别
  17. Java设计模式学习笔记:单例模式(一)
  18. Android百度地图无法定位问题
  19. OSVR头部追踪数据格式及VRPN数据处理流程
  20. SSD固态硬盘接口种类多,你了解多少?

热门文章

  1. vba ado返回集合_利用VBA代码导出工作表中的图片
  2. 转载《Python与开源GIS教程》随书源码网址
  3. SM4国密标准 GB/T 32907-2016
  4. JVM的7种垃圾收集器
  5. 推荐21个顶级的Vue UI库!
  6. java 获取域名_Java获取域名,Java从URL地址中获取域名,Java从Request 获取域名
  7. pycharm python3.7环境_Python3+Pycharm+PyQt5环境搭建步骤图文详解
  8. 金蝶站点重新输入服务器ip,金蝶KIS客户端和服务器不在同一IP段互相访问
  9. 轻量级锁_一句话撸完重量级锁、自旋锁、轻量级锁、偏向锁、悲观、乐观锁等各种锁 不看后悔系列...
  10. MySQL建表(那些字段必须)命令详解