轮胎充气

来源:互联网 发布:js 用法 编辑:程序博客网 时间:2024/05/17 02:24

//我的代码

package xj;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class LunTai {
 static int T, N,limit;
 static int data[][];
 static int a[];
 static boolean used[];
 static int mind;
 static boolean flag;

 public static void main(String[] args) throws FileNotFoundException {
  /* Scanner sc=new Scanner(System.in); */
  Scanner sc = new Scanner(new File("src/LunTai"));
  while(sc.hasNext()){
   N=sc.nextInt();
   limit=sc.nextInt();
   data=new int [N][2];
   used=new boolean[N];
   a=new int[N];
   for (int i = 0; i < N; i++) {
    for (int j = 0; j <2; j++) {
     data[i][j]=sc.nextInt();
    }
   }
   flag=false;
   mind=0xfffffff;
   dfs(0,0,0);
   //
   if(!flag){System.out.println("-1");}
   else{System.out.println(mind);}
  }
 }

 private static void dfs(int step, int sum,int min) {
  if(step==N){
   if(check()){flag=true;}
   if(mind>min){mind=min;}return;
  }
  for (int i = 0; i < N; i++) {
   if(!used[i]){
    //a[step]=i;
    int t=min;
    int k=sum;
    used[i]=true;
    int c=moni(i,sum,min);
    if(c!=-1){
     sum+=data[i][0];
     sum-=data[i][1];
     if(sum<0){min+=-sum;sum=0;}
     dfs(step+1,sum,min);
        sum=k;
        min=t;
        }
    used[i]=false;
   }
  }
 }

 private static boolean check() {
  for (int i = 0; i < N; i++) {
   if(!used[i]){return false;}
  }
  return true;
 }

 private static int moni(int i,int sum, int min) {
  sum+=data[i][0];
  if(sum>limit){return -1;}
  sum-=data[i][1];
  if(sum<0){min+=-sum;sum=0;}
  return min;
 }

}


//波的代码

package com.domain;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public classLunTai {
static int p;
static int[][] data;
static int[] arr;
static int[] used;
static int S;
public static void main(String[] args)throws FileNotFoundException {
Scanner sc=new Scanner(newFileInputStream("src/input.txt"));
int a=sc.nextInt();
while(a--!=0){
    p=sc.nextInt();
data=new int[p][2];
arr=new int[p];
used=new int[p];
S=1000000;
for (int i= 0; i< p; i++){
for (int j= 0; j< 2; j++){
data[i][j]=sc.nextInt();
}
}
DFS(0);
System.out.println(S);
}
}
private static void DFS(int step){
if (step==p){
int c=moni();
if (c!=-1){
if (S>c){
S=c;
}
}

return;
}
for (int i= 0; i< p; i++){
if (used[i]==0){
arr[step]=i;
used[i]=1;
DFS(step+1);
used[i]=0;
}
}
}
private static int moni(){
int a=0;
int i=0;
int m=0;
for(;i<p;i++){
a+=data[arr[i]][0];
if (a>100){
return -1;
}
    a-=data[arr[i]][1];
    if (a<0){
    m+=-a;
    a=m;
i=0;
} 
}
return m;
}   
}


//input


3 100
75 30
45 55
80 95
3 100
30 0
30 0
0 40
3 50
75 30
45 55
80 95
3 50
30 0
30 0
0 40

//output

15
0
-1
10


0 0
原创粉丝点击