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);
}
}
}
题目1100:最短路径
最新推荐文章于 2021-09-05 06:58:14 发布