Coursera 算法二 week 3 Baseball Elimination
这周的作业不需要自己写算法,只需要调用库函数就行,但是有些难以理解,因此用了不少时间。
import edu.princeton.cs.algs4.FlowEdge;
import edu.princeton.cs.algs4.FlowNetwork;
import edu.princeton.cs.algs4.FordFulkerson;
import edu.princeton.cs.algs4.ST;
import edu.princeton.cs.algs4.Queue;
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdOut; public class BaseballElimination
{
private int numberOfTeams;
private ST<String, Integer> st;
private int[] wins;
private int[] losses;
private int[] remaining;
private int[][] against;
private String[] teamName; public BaseballElimination(String filename)
{
st = new ST<String, Integer>();
In in = new In(filename);
numberOfTeams = in.readInt();
wins = new int[numberOfTeams];
losses = new int[numberOfTeams];
remaining = new int[numberOfTeams];
teamName = new String[numberOfTeams];
against = new int[numberOfTeams][numberOfTeams]; for (int i = 0; i < numberOfTeams; i++)
{
String name = in.readString();
st.put(name, i);
wins[i] = in.readInt();
losses[i] = in.readInt();
remaining[i] = in.readInt();
teamName[i] = name;
for (int j = 0; j < numberOfTeams; j++)
against[i][j] = in.readInt();
}
} public int numberOfTeams()
{
return numberOfTeams;
} public Iterable<String> teams()
{
return st.keys();
} public int wins(String team)
{
if (!st.contains(team)) throw new IllegalArgumentException();
int index = st.get(team);
return wins[index];
} public int losses(String team)
{
if (!st.contains(team)) throw new IllegalArgumentException();
int index = st.get(team);
return losses[index];
} public int remaining(String team)
{
if (!st.contains(team)) throw new IllegalArgumentException();
int index = st.get(team);
return remaining[index];
} public int against(String team1, String team2)
{
if (!st.contains(team1) || !st.contains(team2)) throw new IllegalArgumentException();
int index1 = st.get(team1);
int index2 = st.get(team2);
return against[index1][index2];
} private FlowNetwork createNetwork(String team)
{
int index = st.get(team);
int num = numberOfTeams - 1; int V = num + 2 + num * (num - 1) / 2;
int s = V - 2, t = V - 1;
FlowNetwork net = new FlowNetwork(V); for (int i = 0; i < num; i++)
{
if (i < index)
{
double capacity = wins[index] + remaining[index] - wins[i];
FlowEdge edge = new FlowEdge(i, t, capacity);
net.addEdge(edge);
}
else
{
double capacity = wins[index] + remaining[index] - wins[i + 1];
FlowEdge edge = new FlowEdge(i, t, capacity);
net.addEdge(edge);
}
}
int temp = num;
for (int i = 0; i < num - 1; i++)
{
for (int j = i + 1; j < num; j++)
{
FlowEdge edge = new FlowEdge(temp, j, Double.POSITIVE_INFINITY);
net.addEdge(edge); edge = new FlowEdge(temp, i, Double.POSITIVE_INFINITY);
net.addEdge(edge); int a = i, b = j;
if (i >= index) a++;
if (j >= index) b++;
double capacity = against[a][b];
edge = new FlowEdge(s, temp, capacity);
net.addEdge(edge); temp++;
}
} return net;
} public boolean isEliminated(String team)
{
if (!st.contains(team)) throw new IllegalArgumentException();
int index = st.get(team);
int num = numberOfTeams - 1;
int V = num + 2 + num * (num - 1) / 2;
int s = V - 2, t = V - 1; for (int i = 0; i < num; i++)
{
int j = i;
if (j >= index) j++;
if (wins[index] + remaining[index] < wins[j])
return true;
} FlowNetwork net = createNetwork(team); FordFulkerson ff = new FordFulkerson(net, s, t);
for (FlowEdge e : net.adj(s))
if (e.capacity() != e.flow())
return true; return false;
} public Iterable<String> certificateOfElimination(String team)
{
if (!st.contains(team)) throw new IllegalArgumentException();
int index = st.get(team);
int num = numberOfTeams - 1;
int V = num + 2 + num * (num - 1) / 2;
int s = V - 2, t = V - 1;
Queue<String> certificate = new Queue<String>(); boolean isEliminated = false;
for (int i = 0; i < num; i++)
{
int j = i;
if (j >= index) j++;
if (wins[index] + remaining[index] < wins[j])
{
isEliminated = true;
certificate.enqueue(teamName[j]);
}
}
if (isEliminated) return certificate; FlowNetwork net = createNetwork(team); FordFulkerson ff = new FordFulkerson(net, s, t);
for (int i = 0; i < num; i++)
{
if (ff.inCut(i))
{
int j = i;
if (j >= index) j++;
certificate.enqueue(teamName[j]);
isEliminated = true;
}
} if (isEliminated) return certificate; return null;
} public static void main(String[] args) {
BaseballElimination division = new BaseballElimination(args[0]);
for (String team : division.teams()) {
if (division.isEliminated(team)) {
StdOut.print(team + " is eliminated by the subset R = { ");
for (String t : division.certificateOfElimination(team)) {
StdOut.print(t + " ");
}
StdOut.println("}");
}
else {
StdOut.println(team + " is not eliminated");
}
}
}
}
Coursera 算法二 week 3 Baseball Elimination的更多相关文章
- Coursera 算法二 week2 Seam Carving
这周作业设计到的算法是有向无环图的最短路径算法,只需要按照顶点的拓扑顺序去放松顶点即可.而在这个题目中拓扑顺序就是按照行的顺序或列的顺序. 用到的数据结构为一个二维数组picture同来存储每个像素的 ...
- Coursera 算法二 week 5 BurrowsWheeler
本打算周末完成这次作业,但没想到遇到了hard deadline,刚开始看不懂题意,后来发现算法4书上有个类似的问题,才理解了题意.最后晚上加班,上课加班,还好在11:35也就是课程结束前25分钟完成 ...
- Coursera 算法二 week 4 Boggle
这次的作业主要用到了单词查找树和深度优先搜索. 1.在深度优先搜索中,在当前层的递归调用前,将marked数组标记为true.当递归调用返回到当前层时,应将marked数组标记为false.这样既可以 ...
- coursera 算法二 week 1 wordnet
这周的作业可谓是一波三折,但是收获了不少,熟悉了广度优先搜索还有符号图的建立.此外还知道了Integer.MAX_VALUE. SAP: 求v和w的大概思路是对v和w分别广度优先搜索,然后遍历图中每一 ...
- Programming Assignment 3: Baseball Elimination
编程作业三 作业链接:Baseball Elimination & Checklist 我的代码:BaseballElimination.java 问题简介 这是一个最大流模型的实际应用问题: ...
- TensorFlow 入门之手写识别(MNIST) softmax算法 二
TensorFlow 入门之手写识别(MNIST) softmax算法 二 MNIST Fly softmax回归 softmax回归算法 TensorFlow实现softmax softmax回归算 ...
- 分布式共识算法 (二) Paxos算法
系列目录 分布式共识算法 (一) 背景 分布式共识算法 (二) Paxos算法 分布式共识算法 (三) Raft算法 分布式共识算法 (四) BTF算法 一.背景 1.1 命名 Paxos,最早是Le ...
- Floyd算法(二)之 C++详解
本章是弗洛伊德算法的C++实现. 目录 1. 弗洛伊德算法介绍 2. 弗洛伊德算法图解 3. 弗洛伊德算法的代码说明 4. 弗洛伊德算法的源码 转载请注明出处:http://www.cnblogs.c ...
- Dijkstra算法(二)之 C++详解
本章是迪杰斯特拉算法的C++实现. 目录 1. 迪杰斯特拉算法介绍 2. 迪杰斯特拉算法图解 3. 迪杰斯特拉算法的代码说明 4. 迪杰斯特拉算法的源码 转载请注明出处:http://www.cnbl ...
随机推荐
- Unity3d 控制物体移动、旋转、缩放
在Unity中通过利用 Input Manager(输入管理器)可以很简单的实现对一个物体进行移动.旋转.缩放操作. 演示代码: //通过虚拟轴控制物体移动.旋转.缩放 public class Mo ...
- 给 asp.net core 写一个简单的健康检查
给 asp.net core 写一个简单的健康检查 Intro 健康检查可以帮助我们知道应用的当前状态是不是处于良好状态,现在无论是 docker 还是 k8s 还是现在大多数的服务注册发现大多都提供 ...
- JS的几条规则
总结一下,有这么几条规则需要遵守: 不要使用new Number().new Boolean().new String()创建包装对象: 用parseInt()或parseFloat()来转换任意类型 ...
- nodebrew
创建: 2019/05/30 完成: 2019/05/30 安装 安装 curl -L git.io/nodebrew | perl - setup 更新nodebrew nodebrew sel ...
- The Largest Generation (25)(BFS)(PAT甲级)
#include<bits/stdc++.h>using namespace std;int n,m,l,t;int a[1307][137][67];int vis[1307][137] ...
- UPC11073(DP,思维)
#include<bits/stdc++.h>using namespace std;long long dp[507][507];const long long mod = 998244 ...
- hdu3078(lca / RMQ在线)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3078 题意: 给出一棵 n 个点的带点权值的树, 接下来有 q 组形如 k, x, y 的输入, 若 ...
- MCP|WJ|Identification of candidate plasma protein biomarkers for cervical cancer using the multiplex proximity extension assay(利用多重邻位延伸分析技术进行宫颈癌血浆蛋白候选生物标记物的鉴定研究)
文献名:Identification of candidate plasma protein biomarkers for cervical cancer using the multiplex pr ...
- h5自定义播放器得实现原理
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- POJ1046 Color Me Less
题目来源:http://poj.org/problem?id=1046 题目大意: 在RGB颜色空间中,用下面的公式来度量两个颜色值的距离. 现给出16个RGB表示的颜色,和一些用于测试的颜色,求被测 ...