首页 > 专题系列 > Java解POJ > POJ 1724 ROADS [解题报告] Java
2013
11-10

POJ 1724 ROADS [解题报告] Java

ROADS

问题描述 :

N cities named with numbers 1 … N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins).

Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away – to the city N. He wants to get there as quickly as possible, but he is short on cash.

We want to help Bob to find the shortest path from the city 1 to the city N that he can afford with the amount of money he has.

输入:

The first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way.

The second line contains the integer N, 2 <= N <= 100, the total number of cities.

The third line contains the integer R, 1 <= R <= 10000, the total number of roads.

Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :

  • S is the source city, 1 <= S <= N
  • D is the destination city, 1 <= D <= N
  • L is the road length, 1 <= L <= 100
  • T is the toll (expressed in the number of coins), 0 <= T <=100

Notice that different roads may have the same source and destination cities.

输出:

The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins.

If such path does not exist, only number -1 should be written to the output.

样例输入:

5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2

样例输出:

11

解题代码:

//* @author:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

class Node{
    int v, dis, cost, next;
}

public class Main{
    int MAX = 1<< 15;
    Node edge[] = new Node[MAX];
    int head[] = new int[MAX];
    int n, k, len, ans;
    boolean vis[] = new boolean[10000];
   
    void dfs(int v, int dis, int money){
        if(dis>ans)return;
        if(v == n){
            if(money >=0&&dis< ans)
                ans = dis;
        }
        for(int i = head[v];i!=0;i = edge[i].next){
            if(!vis[edge[i].v] && money-edge[i].cost>=0){
                vis[edge[i].v] = true;
                dfs(edge[i].v, dis + edge[i].dis, money - edge[i].cost);
                vis[edge[i].v] = false;
            }
        }
    }
   
    void solve() throws IOException{
       // BufferedReader cin = new BufferedReader(new FileReader(new File("in")));
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        String read;
        k = Integer.parseInt(cin.readLine());
        n = Integer.parseInt(cin.readLine());
        int r = Integer.parseInt(cin.readLine());
        len = 0;
        Arrays.fill(vis, false);
        for(int i=0;i< r;i++){
            read = cin.readLine();
            int s = Integer.parseInt(read.split(" ")[0]);
            int t = Integer.parseInt(read.split(" ")[1]);
            int l = Integer.parseInt(read.split(" ")[2]);
            int c = Integer.parseInt(read.split(" ")[3]);
            if(edge[++len] == null)edge[len] = new Node();
            edge[len].v = t;
            edge[len].cost = c;
            edge[len].dis = l;
            edge[len].next = head[s];
            head[s] = len;
        }
        ans = 1<< 29;
        dfs(1, 0, k);
        if(ans == 1<< 29)System.out.println("-1");
        else System.out.println(ans);
    }
   
    public static void main(String[] args) throws IOException{
        Main test = new Main();
        test.solve();
    }
}


解法二

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;


class Node{
    int num;
    int dist;
    int cost;
}

class Edge{
    int t;
    int next;
    int spent;
    int len;
}

public class Main{
    Edge e[] = new Edge[10005];
    PriorityQueue< Node> q = new PriorityQueue< Node>(100,new Comparator< Node>(){
        @Override
        public int compare(Node o1, Node o2){
            if(o1.dist > o2.dist)return 1;
            if(o1.dist < o2.dist)return -1;
            return 0;
        }
    });

    int head[] = new int[105];
    int num = 1;
   
    void addEdge(int s, int t, int len, int money){
        if(e[num] == null)e[num] = new Edge();
        e[num].t = t;
        e[num].next = head[s];
        e[num].spent = money;
        e[num].len = len;
        head[s] = num++;
    }
   
    void solve() throws IOException{
        BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader cin = new BufferedReader(new FileReader(new File("in")));
        int i, k, n, r, u, tmp = -1;
        Node p, newP;
        String read;
        k = Integer.parseInt(cin.readLine());
        n = Integer.parseInt(cin.readLine());
        r = Integer.parseInt(cin.readLine());
        Arrays.fill(head, 0);
        for(i=0;i< r;i++){
            read = cin.readLine();
            int s = Integer.parseInt(read.split(" ")[0]);
            int d = Integer.parseInt(read.split(" ")[1]);
            int l = Integer.parseInt(read.split(" ")[2]);
            int t = Integer.parseInt(read.split(" ")[3]);
            addEdge(s, d, l, t);
        }
        p = new Node();
        p.num = 1;
        p.dist = p.cost = 0;
        q.offer(p);
        while(q.size()!=0){
            p = q.poll();
            if(p.num == n){
                tmp = p.dist;
                break;
            }
            for(u = head[p.num];u!=0;u = e[u].next){
                if(p.cost+e[u].spent <= k){
                    newP = new Node();
                    newP.num = e[u].t;
                    newP.dist = p.dist+e[u].len;
                    newP.cost = p.cost+e[u].spent;
                    q.offer(newP);
                }
            }
        }
        System.out.println(tmp);
    }
   
    public static void main(String[] args) throws IOException {
        Main test = new Main();
        test.solve();
    }
}

  1. 一开始就规定不相邻节点颜色相同,可能得不到最优解。我想个类似的算法,也不确定是否总能得到最优解:先着一个点,随机挑一个相邻点,着第二色,继续随机选一个点,但必须至少有一个边和已着点相邻,着上不同色,当然尽量不增加新色,直到完成。我还找不到反例验证他的错误。。希望LZ也帮想想, 有想法欢迎来邮件。谢谢