poj 3169&hdu3592(差分约束)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9687 | Accepted: 4647 |
Description
Some cows like each other and want to be within a certain distance
of each other in line. Some really dislike each other and want to be
separated by at least a certain distance. A list of ML (1 <= ML <=
10,000) constraints describes which cows like each other and the
maximum distance by which they may be separated; a subsequent list of MD
constraints (1 <= MD <= 10,000) tells which cows dislike each
other and the minimum distance by which they must be separated.
Your job is to compute, if possible, the maximum possible distance
between cow 1 and cow N that satisfies the distance constraints.
Input
Lines 2..ML+1: Each line contains three space-separated positive
integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must
be at most D (1 <= D <= 1,000,000) apart.
Lines ML+2..ML+MD+1: Each line contains three space-separated
positive integers: A, B, and D, with 1 <= A < B <= N. Cows A
and B must be at least D (1 <= D <= 1,000,000) apart.
Output
1: A single integer. If no line-up is possible, output -1. If cows 1
and N can be arbitrarily far apart, output -2. Otherwise output the
greatest possible distance between cows 1 and N.
Sample Input
4 2 1
1 3 10
2 4 20
2 3 3
Sample Output
27
Hint
There are 4 cows. Cows #1 and #3 must be no more than 10 units
apart, cows #2 and #4 must be no more than 20 units apart, and cows #2
and #3 dislike each other and must be no fewer than 3 units apart.
The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <queue>
using namespace std;
const int M = ;
const int N = ;
const int INF = ;
struct Edge{
int v,w,next;
}edge[M];
int head[N];
int n;
bool vis[N];
int time[N],low[N];
int spfa(int s){
queue<int> q;
for(int i=;i<=n;i++){
vis[i] = false;
low[i] = INF;
time[i] = ;
}
low[s] = ;
time[s]++;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int k = head[u];k!=-;k=edge[k].next){
int v = edge[k].v,w = edge[k].w;
if(low[v]>low[u]+w){
low[v] = low[u]+w;
if(!vis[v]){
vis[v] = true;
q.push(v);
if(time[v]++>n) return -;
}
}
}
}
if(low[n]==INF) return -;
return low[n];
}
void addEdge(int u,int v,int w,int &k){
edge[k].v = v,edge[k].w = w,edge[k].next = head[u],head[u]=k++;
}
int main()
{
int ml,md;
while(scanf("%d%d%d",&n,&ml,&md)!=EOF){
memset(head,-,sizeof(head));
int u,v,w;
int tot = ;
for(int i=;i<ml;i++){
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w,tot);
}
for(int i=;i<md;i++){
scanf("%d%d%d",&u,&v,&w);
addEdge(v,u,-w,tot);
}
for(int i=;i<n;i++){
addEdge(i+,i,-,tot);
}
printf("%d\n",spfa());
}
}
hdu 3592
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <queue>
using namespace std;
const int M = ;
const int N = ;
const int INF = ;
struct Edge
{
int v,w,next;
} edge[M];
int head[N];
int n;
bool vis[N];
int time[N],low[N];
int spfa(int s)
{
queue<int> q;
for(int i=; i<=n; i++)
{
vis[i] = false;
low[i] = INF;
time[i] = ;
}
low[s] = ;
time[s]++;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int k = head[u]; k!=-; k=edge[k].next)
{
int v = edge[k].v,w = edge[k].w;
if(low[v]>low[u]+w)
{
low[v] = low[u]+w;
if(!vis[v])
{
vis[v] = true;
q.push(v);
if(time[v]++>n) return -;
}
}
}
}
if(low[n]==INF) return -;
return low[n];
}
void addEdge(int u,int v,int w,int &k)
{
edge[k].v = v,edge[k].w = w,edge[k].next = head[u],head[u]=k++;
}
int main()
{
int ml,md;
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d%d%d",&n,&ml,&md);
memset(head,-,sizeof(head));
int u,v,w;
int tot = ;
for(int i=; i<ml; i++)
{
scanf("%d%d%d",&u,&v,&w);
addEdge(u,v,w,tot);
}
for(int i=; i<md; i++)
{
scanf("%d%d%d",&u,&v,&w);
addEdge(v,u,-w,tot);
}
for(int i=; i<n; i++)
{
addEdge(i+,i,-,tot);
}
printf("%d\n",spfa());
}
}
poj 3169&hdu3592(差分约束)的更多相关文章
- ShortestPath:Layout(POJ 3169)(差分约束的应用)
布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...
- POJ 3169 Layout (差分约束)
题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...
- POJ 3169 Layout(差分约束+链式前向星+SPFA)
描述 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...
- POJ 3169 Layout(差分约束啊)
题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...
- POJ 3169 Layout(差分约束 线性差分约束)
题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...
- poj 3169 Layout 差分约束模板题
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6415 Accepted: 3098 Descriptio ...
- poj 1201 Intervals(差分约束)
题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...
- poj 1201 Intervals——差分约束裸题
题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...
- POJ——3169Layout(差分约束)
POJ——3169Layout Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14702 Accepted ...
随机推荐
- python练手习题
不断记录python常见习题,不断寻求更多更好的解决办法.持续更新中..... 练习: 1. list两两元素交换位置,如[1,2,3,4,5,6] 执行后为 -> [2,1,4,3,6,5] ...
- IntentService和Service执行子线程对比
1.为何要用子程序 服务是在主线程中执行的,直接在服务中执行耗时操作明显不可取,于是安卓官方增加了IntentService类来方便使用 在Service中执行子程序代码如下 @Override pu ...
- 商品评分效果JavaScript
<script> window.onload=function(){ //----------选中的星星会多出一个属性:isClick="true" 藉此来获取评分-- ...
- 第2章c++简单程序设计
第2章c++简单程序设计 知识梳理 以下是我遗忘以及认为重要的知识整理: 1.标识符的构成规则: 以大写字母.小写字母或下划线 _ 开始 由大写字母.小写字母.下划线 _ 或数字(0~9)组成 大写字 ...
- 线性回归、逻辑回归(LR)
线性回归 回归是一种极易理解的模型,就相当于y=f(x),表明自变量 x 和因变量 y 的关系.最常见问题有如 医生治病时的望.闻.问.切之后判定病人是否生了什么病,其中的望闻问切就是获得自变量x,即 ...
- luoguT30204 偷上网
\(n=1\) 时特判四角,其余时刻圆的面积和必小于正方形面积,随机点出来判断就行了. stm 随机算法-- #include <iostream> #include <cstdli ...
- 1、IOS学习计划
2015年12月10日 -- 2015年12月27日(一共3个周末,12个个工作日) 1.斯坦福公开课(IOS7应用开发) 一共18节课程,通过视频和demo建立感觉 2.千峰的OC课程 一共25节课 ...
- Spring aop报错:com.sun.proxy.$Proxyxxx cannot be cast to yyy
在使用Spring AOP时,遇到如下的错误: Exception in thread "main" java.lang.ClassCastException: com.sun.p ...
- 修改Linux内核参数 减少TIME-WAIT
编辑/etc/sysctl.conf文件 增加 net.ipv4.tcp_syncookies = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_recycle ...
- [BZOJ2036]聪明的阿卑多
[BZOJ2036]聪明的阿卑多 试题描述 也许你从没听说过阿卑多,但你一定知道他爷爷的爷爷的爷爷,那就是聪明绝顶的阿凡提先生.是的,阿卑多也是个聪明的小孩. 一天,阿卑多骑着他的小毛驴,在小镇上晃悠 ...