結果
| 問題 |
No.453 製薬会社
|
| ユーザー |
warachia_
|
| 提出日時 | 2019-06-24 04:49:40 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 68 ms / 2,000 ms |
| コード長 | 5,395 bytes |
| コンパイル時間 | 3,214 ms |
| コンパイル使用メモリ | 80,008 KB |
| 実行使用メモリ | 37,500 KB |
| 最終ジャッジ日時 | 2024-12-30 17:55:12 |
| 合計ジャッジ時間 | 5,177 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 9 |
ソースコード
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.math.BigInteger;
public class Main implements Runnable {
static int mod = 1000000007;
public static void main(String[] args) {
new Thread(null, new Main(), "", 1024 * 1024 * 1024).start();
}
public void run() {
PrintWriter out = new PrintWriter(System.out);
FastScanner sc = new FastScanner();
double c = sc.nextDouble();
double d = sc.nextDouble();
double[][] A = {{0.75,2/(double)7},{0.25,5/(double)7}};
double[] b = {c,d};
double[] z = {-1000,-2000};
OnePhaseSimplex ops = new OnePhaseSimplex(A,b,z);
ops.solve();
out.println(-ops.min());
out.flush();
}
}
class OnePhaseSimplex {
double[][] t; //シンプレックス・タブロー
double[] con; //シンプレックス・タブローのi行目の定数項
int[] basis; //タブローのi行目の基底変数
int N; //A,cの変数の数
int M; //制約式の数
double eps = 0.000001; //許容誤差ε
//変数のindex:
//0-N-1 x0 - xN-1
//N - N+M-1 s0 - sM-1 スラック変数
//N+M z(陽に持たない)
//タブロー
//0-M-1行目 制約式
//M行目 zの式
//Ax<=b(bの要素が全て正)のもとでz=cxを最小化
public OnePhaseSimplex(double[][] A, double[] b, double[] c){
this.N = A[0].length;
this.M = A.length;
this.t = new double[M+1][N+M];
this.con = new double[M+1];
basis = new int[M+1];
for(int i=0;i<M;i++){
for(int j=0;j<N;j++){
t[i][j] = A[i][j];
}
}
for(int i=0;i<M;i++){
t[i][N+i] = 1; //スラック変数
}
for(int i=0;i<N;i++){
t[M][i] = c[i];
}
for(int i=0;i<M;i++){
con[i] = b[i];
}
//con[M] = 0
for(int i=0;i<M;i++){
basis[i] = N+i;
}
basis[M] = N+M; //z
}
//解く
stat solve(){
int id = nextVariable();
while(id!=-1){
double min = Double.POSITIVE_INFINITY;
int minid = -2;
for(int i=0;i<M;i++){
if(t[i][id]>0){
double candidate = con[i]/t[i][id];
if(candidate < min){
min = candidate;
minid = i;
}
}
}
if(minid == -2){ //非有界
return stat.UNBOUNDED;
}
double divisor = t[minid][id];
for(int i=0;i<N+M;i++){
t[minid][i] /= divisor;
}
con[minid] = min;
basis[minid] = id;
for(int i=0;i<=M;i++){
if(i==minid){
continue;
}
double multi = t[i][id];
for(int j=0;j<N+M;j++){
t[i][j] -= t[minid][j]*multi;
}
con[i] -= con[minid]*multi;
}
id = nextVariable();
}
return stat.HAVESOLUTION;
}
//zを最小化するxを返す
double[] argmin(){
double[] solution = new double[N];
for(int i=0;i<M;i++){
if(basis[i]<N){
solution[basis[i]] = con[i];
}
}
return solution;
}
//最小のzを返す
double min(){
return -con[M];
}
//次に増加させる変数を選ぶ
//既に最適解なら-1を返す
int nextVariable(){
double min = 0;
int minid = -1;
for(int i=0;i<N+M;i++){
if(t[M][i] < min){
min = t[M][i];
minid = i;
}
}
return minid;
}
enum stat{
UNBOUNDED,
NOSOLUTION,
HAVESOLUTION
}
}
class FastScanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[ptr++];
else
return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr]))
ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public int[] nextintArray(int n){
int[] a = new int[n];
for(int i=0;i<n;i++){
a[i] = nextInt();
}
return a;
}
public long[] nextlongArray(int n){
long[] a = new long[n];
for(int i=0;i<n;i++){
a[i] = nextLong();
}
return a;
}
public Integer[] nextIntegerArray(int n){
Integer[] a = new Integer[n];
for(int i=0;i<n;i++){
a[i] = nextInt();
}
return a;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
warachia_