题目1100:最短路径

import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;
import java.util.Collections;

class Main
{
	public static final boolean DEBUG = false;
	public static int[][] f;
	public static final int MOD = 100000;
	public static Vector<Integer>[] dis;
	
	public static void init(int n)
	{
		dis = new Vector[n];
		for (int i = 0; i < n; i++) {
			dis[i] = new Vector<Integer>();
		}
	}
	
	public static Vector<Integer> add(Vector<Integer> v, int data)
	{
		for (int i = 0; i < v.size(); i++) {
			if (v.get(i).intValue() == data) { 
				v.set(i, new Integer(data + 1));
				
				Vector<Integer> ans = new Vector<Integer>();
				ans.setSize(v.size());
				Collections.copy(ans, v);
				
				return ans;
			}
		}
		
		Vector<Integer> ans = new Vector<Integer>();
		ans.setSize(v.size());
		Collections.copy(ans, v);
		ans.add(new Integer(data));
		
		return ans;
	}
	
	public static boolean less(Vector<Integer> v1, Vector<Integer> v2)
	{
		Collections.sort(v1);
		Collections.sort(v2);
		
		int s1 = v1.size();
		int s2 = v2.size();
		
		s1--;
		s2--;
		
		while (s1 != 0 && s2 != 0) {
			if (v1.get(s1).intValue() < v2.get(s2).intValue()) return true;
			else if (v1.get(s1).intValue() > v2.get(s2).intValue()) return false;
			else {
				s1--;
				s2--;
			}
		}
		
		if (v1.get(s1).intValue() == v2.get(s2).intValue()) {
			return v1.size() < v2.size();
		} else {
			return v1.get(s1).intValue() < v2.get(s2).intValue();
		}
	}
	public static long exp(long a, long b, long m)
	{
		long p = 1;
		
		while (b > 0) {
			if ((b & 1) != 0) {
				p = p * a % m;
			}
			
			a = a * a % m;
			b >>= 1;	
		}
		
		return p;
	}
	
	public static void dijkstra(int n)
	{
		boolean[] vis = new boolean[n];
		for (int i = 0; i < n; i++) {
			dis[i].clear();
			dis[i].add(new Integer(1000));
		}
		
		for (int i = 0; i < n; i++) {
			if (f[0][i] != -1) {
				dis[i].clear();
				dis[i].add(new Integer(f[0][i]));
			}
		}
		
		vis[0] = true;
		int u = 0;
		
		for (int i = 1; i < n; i++) {
			Vector<Integer> Min = new Vector<Integer>();
			Min.add(new Integer(1000));
			
			for (int j = 0; j < n; j++) {
				if (!vis[j] && less(dis[j], Min)) {
					Min = dis[j];
					u = j;
				}
			}
			
			vis[u] = true;
			
			for (int j = 0; j < n; j++) {
				if (!vis[j] && f[u][j] != -1 && less(add(dis[u], f[u][j]), dis[j])) {
					dis[j] = add(dis[u], f[u][j]);
				}
			}
		}
		
		for (int i = 1; i < n; i++) {
			if (dis[i].size() == 1 && dis[i].get(0) == 1000) {
				System.out.println(-1);
				continue;
			}
			
			long len = 0;
			for (int j = 0; j < dis[i].size(); j++) {
				len += exp(2, dis[i].get(j).intValue(), MOD);
				len %= MOD;
			}
			System.out.println(len);
		}
	}
	
	public static void test(Vector<Integer> a) 
	{
		Vector<Integer> b = new Vector<Integer>();
		b.setSize(a.size());
		Collections.copy(b, a);
		b.add(new Integer(0));
		
		System.out.println(a);
		System.out.println(b);
		
	}
	public static void main(String[] args) throws IOException
	{
		Scanner cin;
		int n, m;
		
		if (DEBUG) {
			cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));
		} else {
			cin = new Scanner(new InputStreamReader(System.in));
		}
		
		while (cin.hasNext()) {
			n = cin.nextInt();
			m = cin.nextInt();
			
			init(n);
			f = new int[n][n];
			for (int i = 0; i < f.length; i++) {
				Arrays.fill(f[i], -1);
			}
			for (int i = 0; i < m; i++) {
				int a = cin.nextInt();
				int b = cin.nextInt();
				f[a][b] = f[b][a] = i;
				//System.out.println("f[a][b]=" + f[a][b]);
			}
			
			dijkstra(n);
		}
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值