Acwing 1129. 热浪 【图论、单源最短路】

德克萨斯纯朴的民众们这个夏天正在遭受巨大的热浪!!!

他们的德克萨斯长角牛吃起来不错,可是它们并不是很擅长生产富含奶油的乳制品。

农夫John此时身先士卒地承担起向德克萨斯运送大量的营养冰凉的牛奶的重任,以减轻德克萨斯人忍受酷暑的痛苦。

John已经研究过可以把牛奶从威斯康星运送到德克萨斯州的路线。

这些路线包括起始点和终点一共有 T 个城镇,为了方便标号为 1 到 T。

除了起点和终点外的每个城镇都由 双向道路 连向至少两个其它的城镇。

每条道路有一个通过费用(包括油费,过路费等等)。

给定一个地图,包含 C 条直接连接 2 个城镇的道路。

每条道路由道路的起点 Rs,终点 Re 和花费 Ci 组成。

求从起始的城镇 Ts 到终点的城镇 Te 最小的总费用。

输入格式
第一行: 4 个由空格隔开的整数: T,C,Ts,Te;

第 2 到第 C+1 行: 第 i+1 行描述第 i 条道路,包含 3 个由空格隔开的整数: Rs,Re,Ci。

输出格式
一个单独的整数表示从 Ts 到 Te 的最小总费用。

数据保证至少存在一条道路。

数据范围
1≤T≤2500,
1≤C≤6200,
1≤Ts,Te,Rs,Re≤T,
1≤Ci≤1000
输入样例:
7 11 5 4
2 4 2
1 4 3
7 2 2
3 4 3
5 7 5
7 3 3
6 1 1
6 3 4
2 4 3
5 6 3
7 2 1
输出样例:
7


  • SPFA模板题
import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int n, m, start, end, idx = 0, INF = 0x3f3f3f3f;
    static class Edge {
        private int v, w, ne;
        public Edge(int v, int w, int ne) {
            this.v = v;
            this.w = w;
            this.ne = ne;
        }
    }
    static Edge[] edges;
    static int[] h, dist;
    static boolean[] st;

    public static void main(String[] args) throws IOException {
        String[] line = br.readLine().split(" ");
        n = Integer.parseInt(line[0]);
        m = Integer.parseInt(line[1]);
        start = Integer.parseInt(line[2]);
        end = Integer.parseInt(line[3]);
        edges = new Edge[m << 1];   // 无向图,边数乘2
        h = new int[n + 1];
        dist = new int[n + 1];
        st = new boolean[n + 1];
        Arrays.fill(h, -1);
        while (m-- > 0) {
            line = br.readLine().split(" ");
            int u = Integer.parseInt(line[0]), v = Integer.parseInt(line[1]), w = Integer.parseInt(line[2]);
            add(u, v, w);
            add(v, u, w);
        }
        out.println(spfa());
        br.close();
        out.close();
    }

    public static void add(int u, int v, int w) {
        edges[idx] = new Edge(v, w, h[u]);
        h[u] = idx++;
    }

    public static int spfa() {
        Arrays.fill(dist, INF);
        dist[start] = 0;
        Queue<Integer> q = new LinkedList<>();
        q.offer(start);
        st[start] = true;
        while (!q.isEmpty()) {
            int t = q.poll();
            st[t] = false;
            for (int i = h[t]; i != -1; i = edges[i].ne) {
                int v = edges[i].v, w = edges[i].w;
                if (dist[t] + w < dist[v]) {
                    dist[v] = dist[t] + w;
                    if (!st[v]) {
                        q.offer(v);
                        st[v] = true;
                    }
                }
            }
        }
        return dist[end];
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值