#include<iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<vector> #include<set> #include<map> #include<string> #include<queue> #include<stack> #include <iomanip> using namespace std; #define LL long long const int INF=0x3f3f3f3f; const int N=106; int head[N],I; struct node { double r,c; int j,next; }side[N*2]; double dist[N]; void add(int i,int j,double r,double c) { side[I].j=j; side[I].c=c; side[I].r=r; side[I].next=head[i]; head[i]=I++; } bool spfa(int s,double k,int n) { int num[N]; bool in[N]; memset(num,0,sizeof(num)); memset(in,false,sizeof(in)); queue<int>qt; for(int i=1;i<=n;++i) dist[i]=0.0; dist[s]=k; qt.push(s); num[s]=1; while(!qt.empty()) { int x=qt.front();qt.pop(); in[x]=false; if(x==s&&dist[x]>k) return true; for(int t=head[x];t!=-1;t=side[t].next) { int j=side[t].j; if(dist[j]<(dist[x]-side[t].c)*side[t].r) { dist[j]=(dist[x]-side[t].c)*side[t].r; while(!in[j]) { ++num[j]; if(num[j]>=n) return true; in[j]=true; qt.push(j); } } } } return false; } int main() { //freopen("data.in","r",stdin); int n,m,s; double k; while(cin>>n>>m>>s>>k) { memset(head,-1,sizeof(head)); I=0; while(m--) { int a,b; double rab,cab,rba,cba; cin>>a>>b>>rab>>cab>>rba>>cba; add(a,b,rab,cab); add(b,a,rba,cba); } if(spfa(s,k,n)) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }把这段c++改为Java
时间: 2024-01-21 15:03:02 浏览: 86
将这段 C++ 代码翻译为 Java 代码如下:
```
import java.io.*;
import java.util.*;
public class Main {
static class Node {
double r, c;
int j, next;
Node(int j, double r, double c, int next) {
this.j = j;
this.r = r;
this.c = c;
this.next = next;
}
}
static final int INF = 0x3f3f3f3f;
static final int N = 106;
static int[] head = new int[N];
static int I;
static Node[] side = new Node[N * 2];
static double[] dist = new double[N];
static void add(int i, int j, double r, double c) {
side[I] = new Node(j, r, c, head[i]);
head[i] = I++;
}
static boolean spfa(int s, double k, int n) {
int[] num = new int[N];
boolean[] in = new boolean[N];
Arrays.fill(num, 0);
Arrays.fill(in, false);
Queue<Integer> qt = new LinkedList<>();
for (int i = 1; i <= n; ++i) {
dist[i] = 0.0;
}
dist[s] = k;
qt.offer(s);
num[s] = 1;
while (!qt.isEmpty()) {
int x = qt.poll();
in[x] = false;
if (x == s && dist[x] > k) {
return true;
}
for (int t = head[x]; t != -1; t = side[t].next) {
int j = side[t].j;
if (dist[j] < (dist[x] - side[t].c) * side[t].r) {
dist[j] = (dist[x] - side[t].c) * side[t].r;
while (!in[j]) {
++num[j];
if (num[j] >= n) {
return true;
}
in[j] = true;
qt.offer(j);
}
}
}
}
return false;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n, m, s;
double k;
while (scanner.hasNext()) {
n = scanner.nextInt();
m = scanner.nextInt();
s = scanner.nextInt();
k = scanner.nextDouble();
Arrays.fill(head, -1);
I = 0;
while (m-- > 0) {
int a = scanner.nextInt();
int b = scanner.nextInt();
double rab = scanner.nextDouble();
double cab = scanner.nextDouble();
double rba = scanner.nextDouble();
double cba = scanner.nextDouble();
add(a, b, rab, cab);
add(b, a, rba, cba);
}
if (spfa(s, k, n)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
scanner.close();
}
}
```
阅读全文