HDU-1534 Schedule Problem

四种约束条件。。照做就行了。。

最长路建图。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <deque>

#define rep(i, l, r) for(int i=l; i<=r; i++)
#define down(i, l, r) for(int i=l; i>=r; i--)
#define N 100000
#define MAX 1<<30

using namespace std;
int read()
{
	int x=0, f=1; char ch=getchar();
	while (ch<‘0‘ || ch>‘9‘) { if (ch==‘-‘) f=-1; ch=getchar(); }
	while (ch>=‘0‘ && ch<=‘9‘) { x=x*10+ch-‘0‘; ch=getchar(); }
	return x*f;
}

struct node{int y, v, n;} e[N*10]; int fir[N], en;
int n, v[N], d[N], c[N];
bool b[N], ans;
char s[50];

void Add(int x, int y, int v) { en++; e[en].y=y, e[en].v=v, e[en].n=fir[x], fir[x]=en; }

int main()
{
	n=read(); int t=0;
	while (n)
	{
		t++;
		rep(i, 1, n) fir[i]=0; en=0; ans=true;
		rep(i, 1, n) v[i]=read();
		scanf("%s", s);
		while (s[0] != ‘#‘)
		{
			int x=read(), y=read();
			if (s[0]==‘S‘ && s[2]==‘S‘) Add(y, x, 0);
			else if (s[0]==‘S‘ && s[2]==‘F‘) Add(y, x, v[y]);
			else if (s[0]==‘F‘ && s[2]==‘S‘) Add(y, x, -v[x]);
			else if (s[0]==‘F‘ && s[2]==‘F‘) Add(y, x, v[y]-v[x]);
			scanf("%s", s);
		}
		deque <int> q;
		rep(i, 1, n) b[i]=1, c[i]=1, d[i]=0, q.push_back(i);
		while (!q.empty())
		{
			int x=q.front(), o=fir[x], y=e[o].y; b[x]=false; q.pop_front();
			if (c[x] > n) { ans=false; break; }
			while (o)
			{
				if (d[y] < d[x]+e[o].v)
				{
					d[y] = d[x]+e[o].v;
					if (!b[y]) b[y]=1, c[y]++, !q.empty()&&d[y]>=d[q.front()] ? q.push_front(y) : q.push_back(y);
				}
				o=e[o].n, y=e[o].y;
			}
		}
		printf("Case %d:\n", t);
		if (!ans) printf("impossible\n"); else
		{
			int a=MAX; rep(i, 1, n) if (a > d[i]) a=d[i]; rep(i, 1, n) printf("%d %d\n", i, d[i]-a);
		}
		printf("\n"); n=read();
	}
	return 0;
}

  

时间: 2024-10-20 11:13:38

HDU-1534 Schedule Problem的相关文章

HDOJ 1534 Schedule Problem 差分约束

差分约数: 求满足不等式条件的尽量小的值---->求最长路---->a-b>=c----> b->a (c) Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1503    Accepted Submission(s): 647 Special Judge Problem Descr

HDU 3549 Flow Problem ( 最大流 -EK 算法)

C++,G++的读取速度差距也太大了 Flow Problem 题意:n,m表示n个点m条有向带权边 问:从1-n最大流多少 裸最大流,拿来练手,挺不错的 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; #define

hdu 6180 Schedule

Schedule Problem Description There are N schedules, the i-th schedule has start time si and end time ei (1 <= i <= N). There are some machines. Each two overlapping schedules cannot be performed in the same machine. For each machine the working time

HDU 3549 Flow Problem 网络最大流问题 Edmonds_Karp算法

题目链接:HDU 3549 Flow Problem Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 8218    Accepted Submission(s): 3824 Problem Description Network flow is a well-known difficult problem f

HDU Hotaru&#39;s problem(Manacher算法+贪心)

manacher算法详见 http://blog.csdn.net/u014664226/article/details/47428293 题意:给一个序列,让求其最大子序列,这个子序列由三段组成,第一段和第二段对称,第一段和第三段一样. 思路:首先利用Manacher算法求出以任意两个相邻元素为中心的回文串长度,用a[i]表示i-1,i为中心的回文串长度的一半, 那么问题就转化成了求最大的x,使得a[i]>=x,a[i+x]>=x,这一步可以贪心来做. 将a[i]从大到小排序(间接排序保留

网络流 HDU 3549 Flow Problem

网络流 HDU 3549 Flow Problem 题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法进行求解,注意的问题有两个: 1. 每次增广的时候,反向流量也要进行更行,一开始没注意,WA了几次 ORZ 2. 对于输入的数据,容量要进行累加更新. // 邻接矩阵存储 #include <bits/stdc++.h> using namespace std; const int INF = 0x7fffffff; const i

hdu 3549 Flow Problem(最大流模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph. Input The first line of input

hdu 3549 Flow Problem (网络最大流)

Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 6674    Accepted Submission(s): 3112 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, yo

HDU1534 Schedule Problem 【差分约束系统】

Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1283    Accepted Submission(s): 534 Special Judge Problem Description A project can be divided into several parts. Each part sho

HDU 1022 Train Problem I 模拟栈题解

火车进站,模拟一个栈的操作,额外的栈操作,查看是否能按照规定顺序出栈. 数据量很少,故此题目很容易AC. 直接使用数组模拟就好. #include <stdio.h> const int MAX_N = 10; char inOrder[MAX_N], outOrder[MAX_N], stk[MAX_N]; bool rs[MAX_N<<2]; int n; int main() { while (scanf("%d", &n) != EOF) { s