POJ #3253 : Fence Repair
传送门:http://poj.org/problem?id=3253
题意:给n个锯完后的木头的长度。每次锯a+b长度的木头花费a+b。问原来的一整块大木头锯完最少花费多少。
思路:贪心,Huffman编码。用优先队列,每次把最小的两块拿出来相加再塞回队列,队列里最后一个元素就是答案。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<utility>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<ctime>
#include<functional>
#include<bitset>
#define P pair<int,int>
#define ll long long
#define ull unsigned long long
#define lson id*2,l,mid
#define rson id*2+1,mid+1,r
#define ls id*2
#define rs (id*2+1)
#define Mod(a,b) a<b?a:a%b+b
#define cl0(a) memset(a,0,sizeof(a))
#define cl1(a) memset(a,-1,sizeof(a))
using namespace std;
const ll M = 1e9 + 7;
const ll INF = 1e9;
const double _e = 10e-6;
const int maxn = 2e4 + 10;
const int matSize = 9;
const int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
const int _dx[8] = { -1,-1,-1,0,0,1,1,1 }, _dy[8] = { -1,0,1,-1,1,-1,0,1 };
int x, y, z;
int n;
struct cmp
{
bool operator()(int a, int b)
{
return a > b;
}
};
void solve()
{
priority_queue<int, vector<int>, cmp> que;
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
que.push(x);
}
ll ans = 0;
while (que.size() > 1) {
int a = que.top(); que.pop();
int b = que.top(); que.pop();
que.push(a + b);
ans += a + b;
}
printf("%lld\n", ans);
}
int main()
{
while (~scanf("%d", &n))
solve();
return 0;
}
POJ #3259 : Wormholes
传送门:http://poj.org/problem?id=3259
题意:给出n个点,m条正的无向边,w条负的有向边。问有没有负环。
思路:Bellman-Ford判断有没有负环。如果有点在第n次还在更新,那就有负环。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<utility>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<ctime>
#include<functional>
#include<bitset>
#define P pair<int,int>
#define ll long long
#define ull unsigned long long
#define lson id*2,l,mid
#define rson id*2+1,mid+1,r
#define ls id*2
#define rs (id*2+1)
#define Mod(a,b) a<b?a:a%b+b
#define cl0(a) memset(a,0,sizeof(a))
#define cl1(a) memset(a,-1,sizeof(a))
using namespace std;
const ll M = 1e9 + 7;
const ll INF = 1e9;
const double _e = 10e-6;
const int maxn = 510;
const int matSize = 9;
const int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
const int _dx[8] = { -1,-1,-1,0,0,1,1,1 }, _dy[8] = { -1,0,1,-1,1,-1,0,1 };
int x, y, z;
struct edge
{
int from, to, cost;
};
int t, n, m, w;
edge e[6010];
bool loop()
{
int d[maxn]; cl0(d);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m * 2 + w; j++) {
edge v = e[j];
if (d[v.to] > d[v.from] + v.cost) {
d[v.to] = d[v.from] + v.cost;
if (i == n)
return true;
}
}
}
return false;
}
int main()
{
scanf("%d", &t);
while (t--) {
scanf("%d%d%d", &n, &m, &w);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &x, &y, &z);
e[i * 2 - 1] = edge{ x,y,z };
e[i * 2] = edge{ y,x,z };
}
for (int i = m * 2 + 1; i <= m * 2 + w; i++) {
scanf("%d%d%d", &x, &y, &z);
e[i] = edge{ x,y,-z };
}
if (loop())
puts("YES");
else
puts("NO");
}
return 0;
}
LightOJ #1074 : Extended Traffic
传送门:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1074
题意:给出n个路口m条有向边,每个路口有一个忙碌值。有q个询问,问从点1到询问的点最小的花费和,若小于3或无法到达则输出‘?’。花费为(终点忙碌值-起点忙碌值)^3。
思路:因为是3次方,所以是可能存在负环的。还是用Bellman-Ford求单源最短路+判哪些点在负环里。
多开一个cycle数组,初始化为0,和算最短路一样不断更新,最终所有cycle[i]<0的点i都在负环里。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<utility>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<ctime>
#include<functional>
#include<bitset>
#define P pair<int,int>
#define ll long long
#define ull unsigned long long
#define lson id*2,l,mid
#define rson id*2+1,mid+1,r
#define ls id*2
#define rs (id*2+1)
#define Mod(a,b) a<b?a:a%b+b
#define cl0(a) memset(a,0,sizeof(a))
#define cl1(a) memset(a,-1,sizeof(a))
using namespace std;
const ll M = 1e9 + 7;
const ll INF = 1e9 + 10;
const double _e = 10e-6;
const int maxn = 210;
const int matSize = 9;
const int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
const int _dx[8] = { -1,-1,-1,0,0,1,1,1 }, _dy[8] = { -1,0,1,-1,1,-1,0,1 };
int x, y, z;
char c;
struct edge
{
int from, to, cost;
};
int t, n, m, q;
int cost[maxn], d[maxn], cycle[maxn];
edge e[maxn*maxn];
int cal(int a, int b)
{
return (b - a)*(b - a)*(b - a);
}
void solve()
{
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
edge v = e[j];
if (d[v.to] > d[v.from] + v.cost) {
d[v.to] = d[v.from] + v.cost;
cycle[v.to] = cycle[v.from] + v.cost;
}
}
}
}
int main()
{
scanf("%d", &t);
for (int _ = 1; _ <= t; _++) {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
d[i] = INF;
d[1] = 0; cl0(cycle);
for (int i = 1; i <= n; i++)
scanf("%d", &cost[i]);
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
e[i] = edge{ x,y,cal(cost[x],cost[y]) };
}
solve();
scanf("%d", &q);
printf("Case %d:\n", _);
while (q--) {
scanf("%d", &x);
if (d[x] < 3 || d[x] == INF || cycle[x] < 0)puts("?");
else printf("%d\n", d[x]);
}
}
return 0;
}
POJ #1949 : Chores
传送门:http://poj.org/problem?id=1949
题意:有n件家务活,每个都有完成需要的时间和前置家务。问全部完成最快要多久。
思路:优先队列,记录点和结束该任务的时间,按时间从小到大排。记每个点的入度,每当入度为0就进队列,不断维护最大值就是答案。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<utility>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<ctime>
#include<functional>
#include<bitset>
#define P pair<int,int>
#define ll long long
#define ull unsigned long long
#define lson id*2,l,mid
#define rson id*2+1,mid+1,r
#define ls id*2
#define rs (id*2+1)
#define Mod(a,b) a<b?a:a%b+b
#define cl0(a) memset(a,0,sizeof(a))
#define cl1(a) memset(a,-1,sizeof(a))
using namespace std;
const ll M = 1e9 + 7;
const ll INF = 1e9 + 10;
const double _e = 10e-6;
const int maxn = 10010;
const int matSize = 9;
const int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
const int _dx[8] = { -1,-1,-1,0,0,1,1,1 }, _dy[8] = { -1,0,1,-1,1,-1,0,1 };
int x, y, z;
char c;
int n, m, ans;
int cost[maxn], in[maxn];
vector<int> son[maxn];
struct node {
int id;
int ans;
};
struct cmp {
bool operator()(node a, node b) {
return a.ans > b.ans;
}
};
void solve()
{
priority_queue<node, vector<node>, cmp> que;
for (int i = 1; i <= n; i++)
if (in[i] == 0)que.push(node{ i,cost[i] });
while (!que.empty()) {
node u = que.top(); que.pop();
ans = max(ans, u.ans);
for (int i = 0; i < son[u.id].size(); i++) {
int v = son[u.id][i];
in[v]--;
if (in[v] == 0)
que.push(node{ v, u.ans + cost[v] });
}
}
}
int main()
{
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++)
son[i].clear();
cl0(in); ans = 0;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &cost[i], &m);
while (m--) {
scanf("%d", &x);
son[x].push_back(i);
in[i]++;
}
}
solve();
printf("%d\n", ans);
}
return 0;
}
POJ #1860 : Currency Exchange
传送门:http://poj.org/problem?id=1860
题意:有n种货币以及m种交易,给定初始时有的货币种类(1种)和货币金额。每种交易都指定2种货币,以及交易的汇率和手续费。问有没有可能钱越换越多。
思路:实际上就是判负环。Bellman-Ford判断即可。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<utility>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<ctime>
#include<functional>
#include<bitset>
#define P pair<int,int>
#define ll long long
#define ull unsigned long long
#define lson id*2,l,mid
#define rson id*2+1,mid+1,r
#define ls id*2
#define rs (id*2+1)
#define Mod(a,b) a<b?a:a%b+b
#define cl0(a) memset(a,0,sizeof(a))
#define cl1(a) memset(a,-1,sizeof(a))
using namespace std;
const ll M = 1e9 + 7;
const ll INF = 1e9;
const double _e = 10e-6;
const int maxn = 1e4 + 10;
const int matSize = 9;
const int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
const int _dx[8] = { -1,-1,-1,0,0,1,1,1 }, _dy[8] = { -1,0,1,-1,1,-1,0,1 };
int x, y, z;
char c;
struct edge
{
int from, to;
double r, c;
};
int n, m, s;
double ss, a1, a2, a3, a4;
edge e[210];
bool loop()
{
double d[maxn]; cl0(d); d[s] = ss;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m * 2; j++) {
edge v = e[j];
if (d[v.to] < (d[v.from] - v.c)*v.r) {
d[v.to] = (d[v.from] - v.c)*v.r;
if (i == n)
return true;
}
}
}
return false;
}
int main()
{
while (~scanf("%d%d%d%lf", &n, &m, &s, &ss)) {
for (int i = 1; i <= m; i++) {
scanf("%d%d%lf%lf%lf%lf", &x, &y, &a1, &a2, &a3, &a4);
e[i * 2 - 1] = edge{ x,y,a1,a2 };
e[i * 2] = edge{ y,x,a3,a4 };
}
if (loop()) puts("YES");
else puts("NO");
}
return 0;
}