Til the Cows Come Home (最短路,Dijkstra算法)

Til the Cows Come Home


Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.
Farmer John’s field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
贝茜在外面的田里,她想在农夫约翰叫醒她去挤奶前回到谷仓,睡个好觉。贝茜需要美美的睡眠,所以她想尽快回来。
农夫约翰的田里有N个(2 <= N <= 1000)地标,唯一编号为1..N。地标1是谷仓;贝西整天站在的那片苹果园是地标N.奶牛在田野里走着,用T (1 <= T <= 2000)双向的牛道——不同长度的牛道。贝茜对自己的导航能力没有信心,所以一旦开始,她就会一直跟踪。
根据路标之间的路径,确定贝西要走的最小距离才能回到谷仓。可以保证存在这样的路线。


Input
* Line 1: Two integers: T and N
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
* 第1行:两个整数:T和N
*线2 . .T+1:每一行描述一个轨迹为三个空格分隔的整数。前两个整数是路径旅行的地标。第三个整数是路径的长度,范围是1..100。


Output
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
*第1行:单个整数,贝西从地标N到地标1的最小距离。


Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100


Sample Output
90


Hint(解释)
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.


#include <stdio.h>
#include<algorithm>
using namespace std; 

const int N = (int) 1000 + 11; 
const int INF = (int) 0x3f3f3f3f;

int n, m; 
int mp[N][N]; 
void init(int n){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(i == j) mp[i][j] = 0;
            else mp[i][j]= INF;         //初始化 
        }
    }
}
void getmap(int m){ 
    int u, v, val;
    while(m--){
        scanf("%d%d%d", &u, &v, &val);
        u--; v--;
        mp[u][v] =  mp[v][u]= min(mp[u][v], val);   //无向图 
    }
}
bool vis[N]; int dis[N];    //vis[]是否走过,dis[]i点到st的最近距离 
void djk(int st, int ed){ 
    for(int i = 0; i < n; i++){
        vis[i] = false;
        dis[i] = mp[st][i];
    } 
    vis[st] = true;
    for(int i = 1; i < n; i++){
        int mn = INF, id = -1;  
        for(int j = 0; j < n; j++){
            if(!vis[j] && dis[j] < mn) {
                mn = dis[j]; id = j;
            }
        }
        if(id == -1) break;
        vis[id] = true; 
        for(int j = 0; j < n; j++){
            if(!vis[j] && mp[id][j] != INF) { 
                if(dis[j] > dis[id] + mp[id][j])  
                    dis[j] = dis[id] + mp[id][j];
            }
        }
    }
    printf("%d\n", (dis[ed] == INF) ? -1 : dis[ed]);
}
int main(){
    int t; 
    scanf("%d%d", &t, &n);
    init(n);
    getmap(t);
    djk(n-1, 0);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
雪花算法是一种生成唯一ID的算法,其特点是不依赖于任何数据库或第三方服务,可以在分布式系统保证ID的唯一性。下面是一个用Python实现雪花算法的示例代码: ```python import time class SnowflakeIDGenerator: def __init__(self, datacenter_id, worker_id): self.twepoch = 1288834974657 self.datacenter_id = datacenter_id self.worker_id = worker_id self.sequence = 0 self.worker_id_bits = 5 self.datacenter_id_bits = 5 self.max_worker_id = -1 ^ (-1 << self.worker_id_bits) self.max_datacenter_id = -1 ^ (-1 << self.datacenter_id_bits) self.sequence_bits = 12 self.worker_id_shift = self.sequence_bits self.datacenter_id_shift = self.sequence_bits + self.worker_id_bits self.timestamp_left_shift = self.sequence_bits + self.worker_id_bits + self.datacenter_id_bits self.sequence_mask = -1 ^ (-1 << self.sequence_bits) self.last_timestamp = -1 def generate_id(self): timestamp = self.time_gen() if timestamp < self.last_timestamp: raise Exception("Clock moved backwards, refusing to generate id") if timestamp == self.last_timestamp: self.sequence = (self.sequence + 1) & self.sequence_mask if self.sequence == 0: timestamp = self.til_next_millis(self.last_timestamp) else: self.sequence = 0 self.last_timestamp = timestamp return ((timestamp - self.twepoch) << self.timestamp_left_shift) | \ (self.datacenter_id << self.datacenter_id_shift) | \ (self.worker_id << self.worker_id_shift) | \ self.sequence def time_gen(self): return int(time.time() * 1000) def til_next_millis(self, last_timestamp): timestamp = self.time_gen() while timestamp <= last_timestamp: timestamp = self.time_gen() return timestamp ``` 使用示例: ```python id_gen = SnowflakeIDGenerator(datacenter_id=1, worker_id=1) print(id_gen.generate_id()) ``` 这个示例生成的ID是一个64位的整数,其前41位是时间戳,接着的5位是数据心ID,5位是工作机器ID,最后的12位是序列号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值