[九度OJ]最短路径

14 篇文章 1 订阅
11 篇文章 0 订阅

时间限制: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 4
1 2
2 3
1 3
0 1
样例输出:
8
9
11
来源:

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 1005
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值