[九度OJ]最短路径

来源:互联网 发布:与sqlserver建立连接 编辑:程序博客网 时间:2024/04/30 05:20

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:5046

解决:766

题目描述:

N个城市,标号从0到N-1,M条道路,第K条道路(K从0开始)的长度为2^K,求编号为0的城市到其他城市的最短距离

输入:

第一行两个正整数N(2<=N<=100)M(M<=500),表示有N个城市,M条道路
接下来M行两个整数,表示相连的两个城市的编号

输出:

N-1行,表示0号城市到其他城市的最短路,如果无法到达,输出-1,数值太大的以MOD 100000 的结果输出。

样例输入:
4 41 22 31 30 1
样例输出:
8911
来源:

2010年上海交通大学计算机研究生机试真题


Dijkstra: 还有问题

import java.math.*;import java.util.*;public class Main {    static final BigInteger TWO = new BigInteger("2");    static final BigInteger MAX = Main.TWO.pow(501);    public static void Dijkstra(BigInteger[][] dist, BigInteger[] cost, int n) {        int[] tag = new int[n];        tag[0] = 1;        int cnt = 1;        while (cnt != n) {            BigInteger minCost = Main.MAX;            int pos = 0;            for (int i = 0; i < n; i++) {                if (tag[i]==0 && cost[i].compareTo(minCost)<0) {                    minCost = cost[i];                    pos = i;                }            }            if (pos == 0) return;            tag[pos] = 1;            cnt++;            cost[pos] = minCost;            for (int i = 0; i < n; i++) {                if (tag[i] == 0) {                    BigInteger tmpCost = (dist[pos][i]==MAX ? MAX:cost[pos].add(dist[pos][i]));                    if (tmpCost.compareTo(cost[i]) < 0) {                        cost[i] = tmpCost;                    }                }            }        }    }    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        int n, m;        n = scanner.nextInt();        m = scanner.nextInt();        BigInteger[][] dist = new BigInteger[n][n];        BigInteger[] cost = new BigInteger[n];        for (int i = 0; i < n; i++) {            for (int j = 0; j < n; j++) {                if (i == j) dist[i][j] = BigInteger.ZERO;                else dist[i][j] = Main.MAX;            }        }        int xpos, ypos;        BigInteger currVal = new BigInteger("1");        for (int i = 0; i < m; i++) {            xpos = scanner.nextInt();            ypos = scanner.nextInt();            dist[xpos][ypos] = currVal;            currVal = currVal.multiply(Main.TWO);        }        cost[0] = BigInteger.ZERO;        for (int i = 1; i < n; i++) {            cost[i] = dist[0][i];        }        Dijkstra(dist, cost, n);        for (int i = 1; i < n; i++) {            if (cost[i].compareTo(MAX) < 0) System.out.println(cost[i]);            else System.out.println("-1");        }        scanner.close();    }}


SPFA: 非本题代码

#include <iostream>#include <cstring>#include <string>#include <fstream>#include <vector>#include <queue>#include <cmath>#define MAXN 1005using namespace std;double city[MAXN][2];int vis[MAXN];int path[MAXN];int A = 144;double orig_cost[MAXN];struct Link{int end;double cost;};vector<Link> vLink[MAXN];double dist(int sp, int ep){double x = city[sp][0] - city[ep][0];double y = city[sp][1] - city[ep][1];return sqrt(double(x*x + y*y));}void output_path(int pos){if (path[pos] != 1)output_path(path[pos]);printf("%d->", path[pos]);}double SPFA(int src,int target){queue<int> QL;QL.push(src);while (!QL.empty()){int origp = QL.front();QL.pop();vis[origp] = 0;for (int i = 0; i < (int)vLink[origp].size(); i++){int currp = vLink[origp][i].end;double tmp_cost = orig_cost[origp] + vLink[origp][i].cost;if (tmp_cost < orig_cost[currp]){orig_cost[currp] = tmp_cost;if (vis[currp] == 0){vis[currp] = 1;QL.push(currp);}path[currp] = origp;}}}return orig_cost[target];}int main(int argc,char **argv){/*if (argc != 3){cerr << "argc error!" << endl;exit(1);}*/freopen("Cities(144).txt", "r", stdin);double px, py;int pcnt;while (scanf("%d %lf %lf\n", &pcnt, &px, &py) != EOF){city[pcnt][0] = px;city[pcnt][1] = py;}for (int i = 2; i <= pcnt; i++)orig_cost[i] = 1e10;memset(vis, 0, sizeof(vis));memset(path, 0, sizeof(path));freopen("Cities(144)link.txt", "r", stdin);int lstart, lend;while (scanf("%d %d\n", &lstart, &lend) != EOF){double c = dist(lstart, lend);vLink[lstart].push_back(Link{ lend, c });vLink[lend].push_back(Link{ lstart, c });}double ans=SPFA(1,A);output_path(A);printf("%d\n%lf\n", A, ans);return 0;}



0 0