您的位置:首页 > 其它

UVa10917 - Walk Through the Forest(单源最短路径及动态规划)

2014-09-09 19:03 337 查看
Jimmy experiences a lot of stress at work these days, especially sincehis accident made working difficult. To relax after a hard day, helikes to walk home. To make things even nicer, his office is on oneside of a forest, and his house is on the other. A
nice walk throughthe forest, seeing the birds and chipmunks is quite enjoyable.

The forest is beautiful, and Jimmy wants to take a different routeeveryday. He also wants to get home before dark, so he always takes apath to make progress towards his house. He considers taking apath fromA toB to be progress if there
existsa route from B to his home that is shorter thanany possible route fromA.Calculate how many different routes through the forest Jimmy might take.

Input

Input contains several test cases followed by a line containing 0.Jimmy has numbered each intersection or joining of paths starting with 1.His office is numbered 1, and his house is numbered 2. Thefirst line of each test case gives the number of intersectionsN,1
< N ≤ 1000, and the number of paths M.The followingM lines each containa pair of intersectionsa b and an integerdistance1 ≤ d ≤ 1000000 indicating a path of lengthdbetween intersectiona and a different
intersection b.Jimmy may walk a path any direction he chooses.There is at most one path between any pair of intersections.

Output

For each test case, output a single integer indicating the number of different routesthrough the forest. You may assume that this number does notexceed 2147483647.

Sample Input

5 6
1 3 2
1 4 2
3 4 3
1 5 12
4 2 34
5 2 24
7 8
1 3 1
1 4 1
3 7 1
7 4 1
7 5 1
6 7 1
5 2 1
6 2 1
0

Output for Sample Input

2
4

用邻接表建图,Dijkstra求出从home到其它点的最短距离,动态规划求出从office到home的可行的路径的个数

10917 -Walk
Through the Forest
 AcceptedJava0.9520.00061015 mins ago
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.util.Scanner;
import java.util.PriorityQueue;
import java.util.Arrays;

public class Main implements Runnable{
private static final boolean DEBUG = false;
private Scanner cin;
private PrintWriter cout;
private int n, m;
private Vertex[] vertexs;
private int[] d;
private boolean[] vis;
private int[] memo;

class Vertex
{
Edge firstEdge;
}

class Edge
{
int from, to, w;
Edge next;
}

class Node implements Comparable<Node>
{
int u, w;

public int compareTo(Node other)
{
return w - other.w;
}
}

private void init()
{
try {
if (DEBUG) {
cin = new Scanner(new BufferedInputStream(new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new Scanner(new BufferedInputStream(System.in));
}

cout = new PrintWriter(new OutputStreamWriter(System.out));
} catch (Exception e) {
e.printStackTrace();
}
}

private boolean input()
{
n = cin.nextInt();
if (n == 0) return false;

vertexs = new Vertex[n + 1];
for (int i = 1; i <= n; i++) {
vertexs[i] = new Vertex();
vertexs[i].firstEdge = null;
}
m = cin.nextInt();

int a, b, d;
for (int i = 0; i < m; i++) {
a = cin.nextInt();
b = cin.nextInt();
d = cin.nextInt();
Edge edge = new Edge();
edge.from = a; edge.to = b; edge.w = d; edge.next = vertexs[a].firstEdge;
vertexs[a].firstEdge = edge;

edge = new Edge();
edge.from = b; edge.to = a; edge.w = d; edge.next = vertexs[b].firstEdge;
vertexs[b].firstEdge = edge;
}
return true;
}

private void Dijkstra(int source)
{
d = new int[n + 1];
vis = new boolean[n + 1];

for (int i = 1; i <= n; i++) d[i] = Integer.MAX_VALUE;
d[source] = 0;

PriorityQueue<Node> pq = new PriorityQueue<Node>();
Node node = new Node();
node.u = source; node.w = d[source];
pq.add(node);

while (!pq.isEmpty()) {
node = pq.poll();
int u = node.u;
if (vis[u]) continue;

vis[u] = true;
for (Edge edge = vertexs[u].firstEdge; edge != null; edge = edge.next) {
int v = edge.to, w = edge.w;
if (d[u] + w < d[v]) {
d[v] = d[u] + w;
node = new Node();
node.u = v;
node.w = d[v];
pq.add(node);
}
}
}
}

private int dp(int x)
{
if (memo[x] != -1) return memo[x];

int ans = 0;
for (Edge edge = vertexs[x].firstEdge; edge != null; edge = edge.next) {
int v = edge.to;
if (d[x] > d[v]) ans += dp(v);
}

return memo[x] = ans;
}

private void solve()
{
Dijkstra(2);

memo = new int[n + 1];
Arrays.fill(memo, -1);
memo[2] = 1;

int ans = dp(1);
cout.println(ans);
cout.flush();
}

public void run()
{
init();

while (input()){
solve();
}
}

public static void main(String[] args)
{
new Thread(new Main()).start();
}
}


第二种方法 ,用SPFA求单源最短路径

10917 -

Walk Through the Forest
 AcceptedJava0.9660.00061514 mins ago
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Arrays;

public class Main implements Runnable{
private static final boolean DEBUG = false;
private Scanner cin;
private PrintWriter cout;
private int n, m;
private Vertex[] vertexs;
private int[] d;
private boolean[] inq;
private int[] memo;

class Vertex
{
Edge firstEdge;
}

class Edge
{
int from, to, w;
Edge next;
}

private void init()
{
try {
if (DEBUG) {
cin = new Scanner(new BufferedInputStream(new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new Scanner(new BufferedInputStream(System.in));
}

cout = new PrintWriter(new OutputStreamWriter(System.out));
} catch (Exception e) {
e.printStackTrace();
}
}

private boolean input()
{
n = cin.nextInt();
if (n == 0) return false;

vertexs = new Vertex[n + 1];
for (int i = 1; i <= n; i++) {
vertexs[i] = new Vertex();
vertexs[i].firstEdge = null;
}
m = cin.nextInt();

int a, b, d;
for (int i = 0; i < m; i++) {
a = cin.nextInt();
b = cin.nextInt();
d = cin.nextInt();
Edge edge = new Edge();
edge.from = a; edge.to = b; edge.w = d; edge.next = vertexs[a].firstEdge;
vertexs[a].firstEdge = edge;

edge = new Edge();
edge.from = b; edge.to = a; edge.w = d; edge.next = vertexs[b].firstEdge;
vertexs[b].firstEdge = edge;
}
return true;
}

private void SPFA(int source)
{
d = new int[n + 1];
inq = new boolean[n + 1];

for (int i = 1; i <= n; i++) d[i] = Integer.MAX_VALUE;
d[source] = 0;
inq[source] = true;

Queue<Integer> queue = new LinkedList<Integer>();
queue.add(source);

while (!queue.isEmpty()) {
int u = queue.poll();
inq[u] = false;

for (Edge edge = vertexs[u].firstEdge; edge != null; edge = edge.next) {
int v = edge.to, w = edge.w;
if (d[u] + w < d[v]) {
d[v] = d[u] + w;
if (!inq[v]) {
inq[v] = true;
queue.add(v);
}
}
}
}
}

private int dp(int x)
{
if (memo[x] != -1) return memo[x];

int ans = 0;
for (Edge edge = vertexs[x].firstEdge; edge != null; edge = edge.next) {
int v = edge.to;
if (d[x] > d[v]) ans += dp(v);
}

return memo[x] = ans;
}

private void solve()
{
SPFA(2);

memo = new int[n + 1];
Arrays.fill(memo, -1);
memo[2] = 1;

int ans = dp(1);
cout.println(ans);
cout.flush();
}

public void run()
{
init();

while (input()){
solve();
}
}

public static void main(String[] args)
{
new Thread(new Main()).start();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: