# Thread: i'm new and help me

1. Member
Join Date
Oct 2010
Posts
1
Rep Power
0

## i'm new and help me

hi i'm new and require some help

i'm new to java and i'm required to change some coding from c++ to java

hope someone here could help :)

#include < algorithm >
#include < iostream >
#include < queue >
#include < vector >
using namespace std;

typedef pair < int, int > ii;
typedef vector < ii > vii;
#define TRvii(c, it) \
for (vii::iterator it = (c).begin(); it != (c).end(); it++)
#define INF 2000000000

int i, u, k, v, weight, V, S;

int main() {
freopen("input_sssp.txt", "r", stdin);

scanf("%d %d", &V, &S); // I read the number of vertices then the starting/source vertex S

for (u = 0; u < V; u++) {
vii Neighbor;
scanf("%d", &k);
while (k--) {
scanf("%d %d", &v, &weight);
Neighbor.push_back(make_pair(v, weight));
}
}

// Run Dijkstra's from source S
vector < int > dist(V, INF); dist[S] = 0;
priority_queue < ii, vector < ii >, greater < ii > > pq;
pq.push(ii(0, S)); // sort based on increasing distance

while (!pq.empty()) { // main loop
ii top = pq.top(); pq.pop(); // greedy: pick shortest unvisited vertex
int d = top.first, u = top.second;
if (d == dist[u]) {
TRvii (AdjList[u], it) { // all outgoing edges from u
int v = it->first, weight_u_v = it->second;
if (dist[u] + weight_u_v < dist[v]) { // if can relax
dist[v] = dist[u] + weight_u_v; // relax
pq.push(ii(dist[v], v)); // enqueue this neighbor
} // regardless it is already in pq or not
}
}
}

for (v = 0; v < V; v++)
printf("Distance from source %d to vertex %d = %d\n", S, v, dist[v]);

return 0;
}

2. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
8
Hello lotatyou, welcome to the forums.

Introductions is not the most appropriate place for questions as it is generally reserved for exactly that.
Still, that aside, have you attempted to convert this code youself at all? Forum members like to see people have a go first.

If you are struggling to get started then I would suggest breaking down the code into its required functionality first.
There are a number of similarities between the languages in terms of functionality though with a few tweaks and changes to naming convension.

A good place to start would be to look at the Java APIs for the following packages:

java.io
java.util.vector
java.util.iterator
java.util.Scanner

Ther are also a number of tutorials to look at.

If you are still struggling after this lot, then post back.

Regards.
Last edited by Ronin; 10-10-2010 at 09:23 PM. Reason: Typo

3. You need to first learn Java, then show the fruits of your effort. Ronin's links are good ones and will set you in the right direction. Best of luck.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•