Today I review the DIJSKRA algorithm. Which is used to get the shortest length of all nodes in a map.
The first step is : to initial the result list 'res' and the sign list 'finRes', which marks whether the shortest was found.
The second step: begin a loop of n-1, every time we will find a shortest path, and set corresponding finRes true. Why n-1, cause we did not need to find v0's.
In the loop : the first thing is to find the shortest length of all res, which finRes are false. Then remember the min value. And take advantage of this min value to find other shorter path and set them.the code:
public static void DIJ(int[][] pMap, int[] res, int v0, int pDim)
{
boolean[] finRes = new boolean[pDim+1];
int min, v;
for(int i = 1; i <= pDim; i ++)
{
res[i] = pMap[v0][i];
finRes[i] = false;
}
res[v0] = 0; finRes[v0] = true;
for(int i = 1; i < pDim; i ++)
{
min = MAX;
v = 0;
for(int j = 1; j <= pDim; j ++)
if(!finRes[j] && res[j] < min)
{
v = j;
min = res[j];
}
finRes[v] = true;
//update other shortest path
for(int k = 1; k <= pDim; k ++)
{
if(!finRes[k] && (min + pMap[k][v] < res[k]))
res[k] = min + pMap[k][v];
}
}
}
Ok. then we will see the question of 1072. Unfortunately, I have not passed all the case recently. Again....
A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range.
Now given the map of the city and several candidate locations for the gas station, you are supposed to give the best recommendation. If there are more than one solution, output the one with the smallest average distance to all the houses. If such a solution is still not unique, output the one with the smallest index number.
Input Specification:
Each input file contains one test case. For each case, the first line contains 4 positive integers: N (<= 103), the total number of houses; M (<= 10), the total number of the candidate locations for the gas stations; K (<= 104), the number of roads connecting the houses and the gas stations; and DS, the maximum service range of the gas station. It is hence assumed that all the houses are numbered from 1 to N, and all the candidate locations are numbered from G1 to GM.
Then K lines follow, each describes a road in the format
P1 P2 Dist
where P1 and P2 are the two ends of a road which can be either house numbers or gas station numbers, and Dist is the integer length of the road.
Output Specification:
For each test case, print in the first line the index number of the best location. In the next line, print the minimum and the average distances between the solution and all the houses. The numbers in a line must be separated by a space and be accurate up to 1 decimal place. If the solution does not exist, simply output “No Solution”.
Sample Input 1:4 3 11 5 1 2 2 1 4 2 1 G1 4 1 G2 3 2 3 2 2 G2 1 3 4 2 3 G3 2 4 G1 3 G2 G1 1 G3 G2 2Sample Output 1:
G1 2.0 3.3Sample Input 2:
2 1 2 10 1 G1 9 2 G1 20Sample Output 2:
No Solution
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Main {
public static final int MAX = 10000000;
public static class GasStation{
String mName;
int[] djDis;
double dAve;
int minDis;
public GasStation(String pName, int pDim)
{
mName = pName;
djDis = new int[pDim+1];
minDis = MAX;
dAve = 0.0;
}
public void GetMinAve(int nHouse, int nMax)
{
int sum = 0;
for(int i = 1; i <= nHouse; i ++)
{
if(djDis[i] > nMax)
return;
if(djDis[i] < minDis)
minDis = djDis[i];
sum += djDis[i];
}
dAve = (double)sum / (double)nHouse;
}
//test
public void printGas(int nHouse)
{
for(int i = 1; i <= nHouse; i ++)
System.out.print(djDis[i] + " ");
System.out.print("\n");
}
}
public static void DIJ(int[][] pMap, int[] res, int v0, int pDim)
{
boolean[] finRes = new boolean[pDim+1];
int min, v;
for(int i = 1; i <= pDim; i ++)
{
res[i] = pMap[v0][i];
finRes[i] = false;
}
res[v0] = 0; finRes[v0] = true;
for(int i = 1; i < pDim; i ++)
{
min = MAX;
v = 0;
for(int j = 1; j <= pDim; j ++)
if(!finRes[j] && res[j] < min)
{
v = j;
min = res[j];
}
finRes[v] = true;
//update other shortest path
for(int k = 1; k <= pDim; k ++)
{
if(!finRes[k] && (min + pMap[k][v] < res[k]))
res[k] = min + pMap[k][v];
}
}
}
public static class CompDis implements Comparator<GasStation>{
public int compare(GasStation g1, GasStation g2) {
// TODO Auto-generated method stub
if(g1.minDis == g2.minDis)
{
if((g1.dAve - g2.dAve) > 0)
return 1;
else if((g1.dAve - g2.dAve) < 0)
return -1;
else
{
return g2.mName.compareTo(g1.mName);
}
}
else
return g2.minDis - g1.minDis;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int nHouse, nGas, nRoads, nDim, maxLen;
Scanner sc = new Scanner(System.in);
nHouse = sc.nextInt();
nGas = sc.nextInt();
nRoads = sc.nextInt();
maxLen = sc.nextInt();
nDim = nHouse + nGas;
int[][] map = new int[nDim + 1][nDim + 1];
int i, j, dis;
//initial
for(int k = 1; k <= nDim; k ++)
for(int m = 1; m <= nDim; m++)
map[k][m] = MAX;
for(int index = 0; index < nRoads; index ++)
{
i = ChgIn(sc.next(), nHouse);
j = ChgIn(sc.next(), nHouse);
dis = sc.nextInt();
map[i][j] = dis;
map[j][i] = dis;
}
//shortest distance
List<GasStation> listGas = new ArrayList<GasStation>();
for(int k = nHouse+1; k <= nDim; k ++)
{
String strName = String.format("G%d", k-nHouse);
GasStation gas = new GasStation(strName, nDim);
DIJ(map, gas.djDis, k, nDim);
gas.GetMinAve(nHouse, maxLen);
listGas.add(gas);
//gas.printGas(nDim);
}
CompDis compDis = new CompDis();
Collections.sort(listGas, compDis);
int kl = 0;
for(; kl < listGas.size(); kl ++)
{
GasStation gas = listGas.get(kl);
if(gas.dAve > 0.0)
{
System.out.print(gas.mName + "\n");
System.out.format("%.1f ", (float)gas.minDis);
System.out.format("%.1f", (float)gas.dAve);
break;
}
}
//
if(kl == listGas.size())
System.out.print("No Solution");
sc.close();
}
public static int ChgIn(String s, int baseNum)
{
int res = 0;
if(s.startsWith("G"))
{
s = s.substring(1, s.length());
res = Integer.parseInt(s);
return res + baseNum;
}
return Integer.parseInt(s);
}
}