差分约束 HDU - 1384 HDU - 3592 HDU - 1531 HDU - 3666
Intervals
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5488 Accepted Submission(s): 1999
Write a program that:
> reads the number of intervals, their endpoints and integers c1, ..., cn from the standard input,
> computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i = 1, 2, ..., n,
> writes the answer to the standard output
Process to the end of file.
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int head[maxn], nex[maxn << ], d[maxn], vis[maxn];
int cnt, n, ans[maxn];
struct node
{
int u, v, w;
}Node[maxn << ]; void add(int u, int v, int w)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].w = w;
nex[cnt] = head[u];
head[u] = cnt++;
} bool spfa(int s)
{
mem(d, -0x3f);
queue<int> Q;
Q.push(s);
vis[s] = ;
d[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
vis[u] = ;
for(int i = head[u]; i != -; i = nex[i])
{
int v = Node[i].v;
if(d[v] < d[u] + Node[i].w)
{
d[v] = d[u] + Node[i].w;
if(!vis[v])
{
Q.push(v);
vis[v] = ;
if(++ans[v] > n) return ;
}
}
}
}
return ;
} int main()
{ while(~scanf("%d", &n))
{
mem(head, -);
mem(vis, );
mem(ans, );
cnt = ;
int u, v, w;
int mx = -INF, mi = INF;
for(int i = ; i <= n; i++)
{
rd(u), rd(v), rd(w);
u++, v++;
mx = max(mx, v);
mi = min(mi, u);
add(u - , v, w);
}
for(int i = mi; i <= mx; i++)
{
// add(mi, i, 0);
add(i - , i, );
add(i, i - , -);
} spfa(mi - ); pd(d[mx]); } return ;
}
World Exhibition
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2440 Accepted Submission(s): 1171
There is something interesting. Some 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 X (1 <= X <= 10,000) constraints describes which person like each other and the maximum distance by which they may be separated; a subsequent list of Y constraints (1 <= Y <= 10,000) tells which person 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 person 1 and person N that satisfies the distance constraints.
The next line: Three space-separated integers: N, X, and Y.
The next X lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= N. Person A and B must be at most C (1 <= C <= 1,000,000) apart.
The next Y lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= C. Person A and B must be at least C (1 <= C <= 1,000,000) apart.
4 2 1
1 3 8
2 4 15
2 3 4
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int head[maxn], ans[maxn], vis[maxn];
int n, m, cnt, s;
int d[maxn]; struct node
{
int v, next;
int w;
}Node[maxn*]; void add(int u, int v, int w)
{
Node[cnt].v = v;
Node[cnt].w = w;
Node[cnt].next = head[u];
head[u] = cnt++;
} int spfa()
{
// mem(vis, 0);
for(int i = ; i < maxn; i++) d[i] = INF;
queue<int> Q;
mem(ans, );
Q.push(s);
vis[s] = ;
d[s] = ;
// mem(vis, 0);
// for(int i=1; i<=n; i++)
// {
// Q.push(i);
// d[i] = INF;
// vis[i] = 1;
// }
while(!Q.empty())
{
int u = Q.front(); Q.pop();
vis[u] = ;
for(int i=head[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(d[e.v] > d[u] + e.w)
{
d[e.v] = d[u] + e.w;
if(!vis[e.v])
{
Q.push(e.v);
vis[e.v] = ;
if(++ans[e.v] > n) return ;
}
}
}
}
return ;
} void init()
{
mem(head, -);
cnt = ;
} bool check(int x)
{
bool flag = ;
for(int i = ; i < cnt; i++)
Node[i].w -= x;
if(spfa())
flag = ;
for(int i = ; i < cnt; i++)
Node[i].w += x;
return flag;
} int main()
{
int x;
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d", &n, &m, &x);
init();
int u, v, w, l = , r = ;
s = ;
rap(i, , m)
{
scanf("%d%d%d", &u, &v, &w);
if(u > v) swap(u, v);
add(u, v, w);
}
rap(i, , x)
{
scanf("%d%d%d", &u, &v, &w);
if(u > v) swap(u, v);
add(v, u, -w);
}
if(spfa()) printf("-1\n");
else if(d[n] == INF)
printf("-2\n");
else
printf("%d\n", d[n]); } return ;
}
King
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2348 Accepted Submission(s): 1052
Unfortunately, as it used to happen in royal families, the son was a little retarded. After many years of study he was able just to add integer numbers and to compare whether the result is greater or less than a given integer number. In addition, the numbers had to be written in a sequence and he was able to sum just continuous subsequences of the sequence.
The old king was very unhappy of his son. But he was ready to make everything to enable his son to govern the kingdom after his death. With regards to his son's skills he decided that every problem the king had to decide about had to be presented in a form of a finite sequence of integer numbers and the decision about it would be done by stating an integer constraint (i.e. an upper or lower limit) for the sum of that sequence. In this way there was at least some hope that his son would be able to make some decisions.
After the old king died, the young king began to reign. But very soon, a lot of people became very unsatisfied with his decisions and decided to dethrone him. They tried to do it by proving that his decisions were wrong.
Therefore some conspirators presented to the young king a set of problems that he had to decide about. The set of problems was in the form of subsequences Si = {aSi, aSi+1, ..., aSi+ni} of a sequence S = {a1, a2, ..., an}. The king thought a minute and then decided, i.e. he set for the sum aSi + aSi+1 + ... + aSi+ni of each subsequence Si an integer constraint ki (i.e. aSi + aSi+1 + ... + aSi+ni < ki or aSi + aSi+1 + ... + aSi+ni > ki resp.) and declared these constraints as his decisions.
After a while he realized that some of his decisions were wrong. He could not revoke the declared constraints but trying to save himself he decided to fake the sequence that he was given. He ordered to his advisors to find such a sequence S that would satisfy the constraints he set. Help the advisors of the king and write a program that decides whether such a sequence exists or not.
1 2 gt 0
2 2 lt 2
1 2
1 0 gt 0
1 0 lt 0
0
successful conspiracy
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int head[maxn], ans[maxn], vis[maxn];
int n, m, cnt, s;
int d[maxn]; struct node
{
int v, next;
int w;
}Node[maxn*]; void add(int u, int v, int w)
{
Node[cnt].v = v;
Node[cnt].w = w;
Node[cnt].next = head[u];
head[u] = cnt++;
} int spfa()
{
mem(vis, );
mem(d, 0x3f);
queue<int> Q;
mem(ans, ); for(int i=; i<=n; i++)
{
Q.push(i);
d[i] = ;
vis[i] = ;
}
while(!Q.empty())
{
int u = Q.front(); Q.pop();
vis[u] = ;
for(int i=head[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(d[e.v] > d[u] + e.w)
{
d[e.v] = d[u] + e.w;
if(!vis[e.v])
{
Q.push(e.v);
vis[e.v] = ;
if(++ans[e.v] > n) return ;
}
}
}
}
return ;
} void init()
{
mem(head, -);
cnt = ;
} bool check(int x)
{
bool flag = ;
for(int i = ; i < cnt; i++)
Node[i].w -= x;
if(spfa())
flag = ;
for(int i = ; i < cnt; i++)
Node[i].w += x;
return flag;
} int main()
{
int x;
while(scanf("%d", &n) && n)
{
scanf("%d", &m);
init();
int u, v, w, l = , r = ;
s = n + ;
char str[];
rap(i, , m)
{
scanf("%d%d%s%d", &u, &v, str, &w);
if(str[] == 'g')
add(u - , u + v, - - w);
else
add(u + v, u - , w - );
} bool flag = spfa();
if(flag)
cout << "successful conspiracy" << endl;
else
cout << "lamentable kingdom" << endl; } return ;
}
THE MATRIX PROBLEM
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9774 Accepted Submission(s): 2508
Each case includes two parts, in part 1, there are four integers in one line, N,M,L,U, indicating the matrix has N rows and M columns, L is the lowerbound and U is the upperbound (1<=N、M<=400,1<=L<=U<=10000). In part 2, there are N lines, each line includes M integers, and they are the elements of the matrix.
2 3 4
8 2 6
5 2 9
可以得出要求是 L <= num[i][j] * a[i] / b[j] <= U
可以转换一下变成
log(L / num[i][j]) <= log(a[i]) - log(b[i]) <= log(U / num[i][j])
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int n, m, s;
int head[maxn], cnt, nex[maxn << ], vis[maxn];
int ans[maxn];
double d[maxn];
struct node
{
int u, v;
double w;
}Node[maxn << ]; void add(int u, int v, double w)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].w = w;
nex[cnt] = head[u];
head[u] = cnt++;
} int spfa()
{
for(int i = ; i < maxn; i++) d[i] = INF;
mem(ans, );
deque<int> Q;
Q.push_front(s);
mem(vis, );
vis[s] = ;
d[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop_front();
vis[u] = ;
for(int i = head[u]; i != -; i = nex[i])
{
int v = Node[i].v;
if(d[v] > d[u] + Node[i].w)
{
d[v] = d[u] + Node[i].w;
if(!vis[v])
{
if(Q.empty()) Q.push_front(v);
else if(d[v] < d[Q.front()]) Q.push_front(v);
else Q.push_back(v);
vis[v] = ;
if(++ans[v] > n) return ;
}
}
}
}
return ;
} int main()
{
int l, r;
while(scanf("%d%d%d%d", &n, &m, &l, &r) != EOF)
{
mem(head, -);
cnt = ;
int u, v, x;
s = ;
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j ++)
{
rd(x);
add(i, n + j, -log(l / (double) x));
add(n + j, i, log(r / (double) x));
} if(spfa())
cout << "NO" << endl;
else cout << "YES" << endl; } return ;
}
差分约束 HDU - 1384 HDU - 3592 HDU - 1531 HDU - 3666的更多相关文章
- hdu 1531(差分约束)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1531 差分约束的题之前也碰到过,刚好最近正在进行图论专题的训练,就拿来做一做. ①:对于差分不等式,a ...
- POJ 3169 Layout (HDU 3592) 差分约束
http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...
- hdu 1384 Intervals (差分约束)
Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 1384 Intervals (差分约束)
Problem - 1384 好歹用了一天,也算是看懂了差分约束的原理,做出第一条查分约束了. 题意是告诉你一些区间中最少有多少元素,最少需要多少个元素才能满足所有要求. 构图的方法是,(a)-> ...
- HDU 1384 Intervals【差分约束-SPFA】
类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b&l ...
- hdu 1531 king(差分约束)
King Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- POJ 1364 / HDU 3666 【差分约束-SPFA】
POJ 1364 题解:最短路式子:d[v]<=d[u]+w 式子1:sum[a+b+1]−sum[a]>c — sum[a]<=sum[a+b+1]−c−1 ...
- hdu 1534 Schedule Problem (差分约束)
Schedule Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- I - 动物狂想曲 HDU - 6252(差分约束)
I - 动物狂想曲 HDU - 6252 雷格西桑和路易桑是好朋友,在同一家公司工作.他们总是一起乘地铁去上班.他们的路线上有N个地铁站,编号从1到N.1站是他们的家,N站是公司. 有一天,雷格西桑起 ...
随机推荐
- DSAPI 3张图片实现花开动画
效果图 素材 代码 Dim B0, B1, B3 As Bitmap Private B As Bitmap = Nothing Private Sub Loading_Load(sender As ...
- Mysql、SqlServer、Oracle三大数据库的区别
一.MySQL 优点: 体积小.速度快.总体拥有成本低,开源: 支持多种操作系统: 是开源数据库,提供的接口支持多种语言连接操作 : MySQL的核心程序采用完全的多线程编程.线程是轻量级的进程,它可 ...
- 38.QT-QAxObject快速写入EXCEL示例
参考链接:https://blog.csdn.net/czyt1988/article/details/52121360 http://blog.sina.com.cn/s/blog_a6fb6cc9 ...
- 对HTML5标签的认识(三)
这篇随笔继续来认识HTML标签.这次随笔主要是对<table>标签的认识和最近我学习到的一些标签来和大家分享. 一.<table>标签 <table>标签的作用主要 ...
- ASP.NET WebApi系列
ASP.NET Web API 是一种框架,用于轻松构建可以访问多种客户端(包括浏览器和移动设备)的 HTTP 服务. ASP.NET Web API 是一种用于在 .NET Framework 上构 ...
- 吐血bug-- 多个input框接连blur事件导致alert接连弹出
本来今天想记录一下Flow在vue源码中的应用,结果临时触发了个bug... bug描述: elementUi + Vue 技术 需求:一个表格中有至少两条数据,每条数据都有input框,在失去焦点后 ...
- 【广州.NET社区线下活动】云定未来 - Azure Meetup
第2届 广州.NET线下沙龙 Azure Meetup 4月13日,第2届广州.NET线下沙龙在广州银行大厦7楼中创学院路演大厅成功举办.来自微软MVP.网易的技术专家们带来了干货满满的知识分享,即使 ...
- 【设计模式】桥接模式 Bridge Pattern
开篇还是引用吕振宇老师的那篇经典的文章<设计模式随笔-蜡笔与毛笔的故事>.这个真是太经典了,没有比这个例子能更好的阐明桥接模式了,这里我就直接盗来用了. 现在市面上卖的蜡笔很多,各种型号, ...
- Vue 入门之目录结构介绍
Vue 是一套用于构建用户界面的渐进式框架,与其它大型的页面框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅易于上手,还便于与第三方库或既有项目整合.另一方面,当 ...
- ORA-04030: out of process memory when trying to allocate 152 bytes (Logminer LCR c,krvtadc)
今天使用LogMiner找回误更新的数据时,查询v$logmnr_contents时,遇到了"ORA-04030: out of process memory when trying to ...