给定的有n个顶点和m条边的有向图中,求出s到t的最短距离。
给定的有n个顶点和m条边的有向图中,求出s到t的最短距离。
输入第一行,四个整数n,m,s,t;
接下来m行,每行三个整数a,b,c,表示有条连接a到b的边,长度为c。
注意重边的情况。对于100%的数据,。边权
。
输出s到t的最短距离,要是s无法到t,则输出-1。
3 3 1 3 1 3 3 1 2 1 2 3 1
2
import java.io.*;
import java.util.*;
public class Main {
static int n;
static int[] h;
static int[] e;
static int[] ne;
static int idx = 0;
static int[] w;
static int m;
static int[] dis;
static boolean[] vs;
public static void add(int x,int y,int val){
e[idx] = y;
ne[idx] = h[x];
w[idx] = val;
h[x] = idx++;
}
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String[] line = buf.readLine().split(" ");
n = Integer.parseInt(line[0]);
m = Integer.parseInt(line[1]);
int s = Integer.parseInt(line[2]);
int t = Integer.parseInt(line[3]);
h = new int[n + 10];
e = new int[100010];
ne = new int[100010];
w = new int[100010];
dis = new int[n + 10];
vs = new boolean[n + 10];
Arrays.fill(h,-1);
for (int i = 0; i < m; i++){
line = buf.readLine().split(" ");
int x = Integer.parseInt(line[0]);
int y = Integer.parseInt(line[1]);
int val = Integer.parseInt(line[2]);
add(x,y,val);
}
int res = dij(s,t);
System.out.println(res);
}
public static int dij(int s,int t){
Arrays.fill(dis,Integer.MAX_VALUE >> 1);
dis[s] = 0;
for (int i = 1; i <= n; i++){
int node = -1;
for (int j = 1; j <= n; j++){
if (!vs[j] && (node == -1 || dis[j] < dis[node])){
node = j;
}
}
vs[node] = true;
for (int j = h[node]; j != -1; j = ne[j]){
int child = e[j];
if (!vs[child]){
dis[child] = Math.min(dis[child],dis[node] + w[j]);
}
}
}
if (dis[t] >= (Integer.MAX_VALUE >> 1)) return -1;
return dis[t];
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()){
int n = in.nextInt();
int m = in.nextInt();
int start = in.nextInt();
int end = in.nextInt();
HashMap<Integer,Node> nodes = new HashMap<>();
for (int i = 0; i < m; i++) {
int from = in.nextInt();
int to = in.nextInt();
int weight = in.nextInt();
if(!nodes.containsKey(from)){
nodes.put(from,new Node(from));
}
if(!nodes.containsKey(to)){
nodes.put(to,new Node(to));
}
Node fromNode = nodes.get(from);
Node toNode = nodes.get(to);
Edge newEdge = new Edge(weight,fromNode,toNode);
fromNode.nexts.add(toNode);
fromNode.edges.add(newEdge);
}
System.out.println(getShortestPath(nodes,start,end));
}
in.close();
}
public static int getShortestPath(HashMap<Integer,Node> nodes, int start, int end){
HashMap<Node,Integer> distanceMap = new HashMap<>();
distanceMap.put(nodes.get(start),0);
HashSet<Node> selectedNodes = new HashSet<>();
Node minNode = getMinDistanceAndUnselectedNode(distanceMap,selectedNodes);
while (minNode!=null){
int distance = distanceMap.get(minNode);
for(Edge edge : minNode.edges){
Node toNode = edge.to;
if(!distanceMap.containsKey(toNode)){
distanceMap.put(toNode,distance+edge.weight);
}else {
distanceMap.put(toNode,Math.min(distanceMap.get(toNode),distance+edge.weight));
}
}
selectedNodes.add(minNode);
minNode = getMinDistanceAndUnselectedNode(distanceMap,selectedNodes);
}
return distanceMap.get(nodes.get(end));
}
private static Node getMinDistanceAndUnselectedNode(HashMap<Node, Integer> distanceMap, HashSet<Node> touchedNodes){
Node minNode = null;
int minDistance = Integer.MAX_VALUE;
for(Map.Entry<Node,Integer> entry : distanceMap.entrySet()){
Node node = entry.getKey();
int distance = entry.getValue();
if(!touchedNodes.contains(node) && distance<minDistance){
minNode = node;
minDistance = distance;
}
}
return minNode;
}
static class Node{
int id;
ArrayList<Node> nexts;
ArrayList<Edge> edges;
public Node(int id) {
this.id = id;
this.nexts = new ArrayList<>();
this.edges = new ArrayList<>();
}
}
static class Edge{
int weight;
Node from;
Node to;
public Edge(int weight, Node from, Node to) {
this.weight = weight;
this.from = from;
this.to = to;
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int m = input.nextInt();
int[][] matrix = new int[n + 1][n + 1];
int from = input.nextInt();
int to = input.nextInt();
for (int i = 0; i < m; i++) {
int r = input.nextInt();
int c = input.nextInt();
if (matrix[r][c] != 0) matrix[r][c] = Math.min(matrix[r][c], input.nextInt());
else matrix[r][c] = input.nextInt();
}
input.close();
if(n==0||m==0||from>n||to>n){
System.out.println(-1);
return;
}
dijkstra(matrix,from,to);
System.out.println(matrix[from][to]==0?-1:matrix[from][to]);
}
public static void dijkstra(int[][] matrix, int source,int target) {
int[] dist = new int[matrix.length];
dist[source]++;
int min = Integer.MAX_VALUE;
int transI = 0;
int minI = 0;
for (int l = 1; l < matrix.length; l++) {
for (int i = 1; i < matrix.length; i++) {
if (dist[i] == 0) {
if (matrix[source][transI] != 0 && matrix[transI][i] != 0) {
if (matrix[source][i] != 0) {
matrix[source][i] = Math.min(matrix[source][i], matrix[source][transI] + matrix[transI][i]);
} else matrix[source][i] = matrix[source][transI] + matrix[transI][i];
}
if ( matrix[source][i]!=0&&min > matrix[source][i]) {
minI = i;
min = matrix[source][i];
}
}
}
transI = minI;
dist[transI]++;
min = Integer.MAX_VALUE;
if(transI==target) return;
}
}
}
#include<iostream>
#include<stdio.h>
#include<vector>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;
struct edg{
int v;
int w;
};
const int max_n=10001;
const int INF=100000;
int n,m,s,t;
bool vis[max_n];
vector<int> tempath;
vector<string> res;
bool flag;
void Dfs(int d,vector<int> pre[max_n],vector<edg> G[max_n]){
if(d==s){
tempath.push_back(d);
string str="";
for(int i=tempath.size()-1;i>=0;i--){
str+=tempath[i]+'0';
}
flag=1;
res.push_back(str);
tempath.pop_back();//无参数!!
return;
}
tempath.push_back(d);
for(int i=0;i<pre[d].size();i++){
Dfs(pre[d][i],pre,G);
}
tempath.pop_back();
}
int djs(int d[],vector<int> pre[max_n],vector<edg> G[max_n]){
for(int i=1;i<=n;i++){
int u=-1,min=INF;
for(int j=1;j<=n;j++){
if(vis[j]==false && min>d[j]){
u=j;
min=d[j];
}
}
if(u==-1){
return -1;
}
vis[u]=true;
for(int i=0;i<G[u].size();i++){
int v=G[u][i].v;
if(vis[v]==false && d[u]+G[u][i].w<d[v]){
d[v]=d[u]+G[u][i].w;
pre[v].clear();
pre[v].push_back(u);
}else if(vis[v]==false && d[u]+G[u][i].w==d[v] ){
pre[v].push_back(u);
}
}
}
}
int main(){
freopen("D:\\in.txt","r",stdin);
while(cin>>n>>m>>s>>t){
vector<int> pre[max_n];
vector<edg> G[max_n];
flag=0;
vis[max_n]=false;
int d[max_n];
fill(d,d+max_n,INF);
d[s]=0;
for(int i=0;i<m;i++){
int k,j,weight;
edg e1,e2;
cin>>k>>j>>weight;
e1.v=j;
e1.w=weight;
G[k].push_back(e1);
}
djs(d,pre,G);
Dfs(t,pre,G);
sort(res.begin(),res.end());
if(flag==0){
cout<<"-1"<<endl;
}else{
cout<<d[t]<<endl;
}
tempath.clear();
res.clear();
}
return 0;
}