LeetCode:787. Cheapest Flights Within K Stops

There are n cities connected by m flights. Each flight starts from city u and arrives at v with a price w.

Now given all the cities and flights, together with starting city src and the destination dst, your task is to find the cheapest price from src to dst with up to k stops. If there is no such route, output -1.

Example 1:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
Output: 200
Explanation:
The graph looks like this:


The cheapest price from city 0 to city 2 with at most 1 stop costs 200, as marked red in the picture.

Example 2:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 0
Output: 500
Explanation:
The graph looks like this:


The cheapest price from city 0 to city 2 with at most 0 stop costs 500, as marked blue in the picture.

 

Constraints:

    The number of nodes n will be in range [1, 100], with nodes labeled from 0 to n - 1.
    The size of flights will be in range [0, n * (n - 1) / 2].
    The format of each flight will be (src, dst, price).
    The price of each flight will be in the range [1, 10000].
    k is in the range of [0, n - 1].
    There will not be any duplicated flights or self cycles.

 

解法1: Dijkstra 算法:

#define MAX_COST (10000 * 100)
struct node 
{
    int stops;
    int cost;
    int index;
};

struct node **g_root;
int g_size;

void priority_queue_init(int size)
{
    g_root = (struct node **)malloc(sizeof(struct node *) * size * 2 );
    g_size = 0;
}

void priority_queue_push( struct node * item)
{
    int current_idx = g_size++;
    while(current_idx > 0)
    {
        int parent_idx  = (current_idx -1) /2 ;
        if(g_root[parent_idx]->cost > item->cost)
        {
            g_root[current_idx] = g_root[parent_idx];
            current_idx = parent_idx;
        }
        else
        {
            break;
        }
    }
    g_root[current_idx] = item;
}

struct node * priority_queue_pop(void)
{
    struct node *ret = g_root[0];
    struct node *tmp = g_root[--g_size];
    int a;
    int b;
    int parent_idx = 0;
    while( 2 * parent_idx + 1 < g_size)
    {
        a = 2 * parent_idx + 1;
        b = 2 * parent_idx + 2;
        if( b < g_size && g_root[b]->cost < g_root[a]->cost)
        {
            a = b;
        }

        if( g_root[a]->cost < tmp->cost)
        {
            g_root[parent_idx] = g_root[a];
            parent_idx = a;
        }
        else
        {
            break;
        }
    }

    g_root[parent_idx] = tmp;

    return ret;
}

bool priority_queue_is_empty(void)
{
    return g_size == 0;
}

struct hash
{
    int key;
    int cost;
    struct hash *next;
};

struct hash *g_hash_root;
int g_hash_size;
void hash_init(int size)
{
    g_hash_root = (struct hash *)malloc(sizeof(struct hash ) * size);
    int i;
    for(i = 0; i < size; ++i)
    {
        g_hash_root[i].next = 0;
        g_hash_root[i].key = -1;
    }

    g_hash_size = size;
}

void hash_set(int key, int cost)
{
    int idx = key % g_hash_size;
    struct hash *iter = g_hash_root[idx].next;
    while(iter && iter->key != key)
    {
        iter = iter->next;
    }
    if(!iter)
    {
        struct hash *new_node = (struct hash *)malloc(sizeof(struct hash));
        new_node->key = key;
        new_node->cost = cost;
        new_node->next = g_hash_root[idx].next;
        g_hash_root[idx].next = new_node;
    }
    else
    {
        iter->cost = cost;
    }
}

 int hash_search(int key)
{
    int hash_idx = key%g_hash_size;
    struct hash *iter = g_hash_root[hash_idx].next;
    while(iter && iter->key != key)
    {
        iter = iter->next;
    }
    if(!iter)
    {
        return MAX_COST;
    }
    else
    {
        return iter->cost;
    }
}







int findCheapestPrice(int n, int** flights, int flightsSize, int* flightsColSize, int src, int dst, int K){

    int ** graph = (int **)malloc(sizeof(int *) * n);
    int i;
    for(i = 0; i < n; ++i)
    {
        graph[i] = (int *)malloc(sizeof(int) * n);
        int j;
        for(j = 0; j < n; ++j)
        {
            graph[i][j] = 0;
        }
    }

    for( i = 0; i < flightsSize; ++i)
    {
        graph[ flights[i][0]][ flights[i][1]] = flights[i][2];
    }

    int *cost_array = (int *)malloc(sizeof(int) * n);
    for(i = 0; i < n; ++i)
    {
        cost_array[i] = MAX_COST;
    }
    cost_array[src] = 0;

    struct node *item = (struct node *)malloc(sizeof(struct node));
    item->stops = 0;
    item->cost = 0;
    item->index = src;

    priority_queue_init(flightsSize + 1);
    hash_init(flightsSize);

    priority_queue_push(item);

    while( !priority_queue_is_empty())
    {
        struct node *item = priority_queue_pop();

        if(item->stops > K + 1)
        {
            continue;
        }
        
        int ret_cost = hash_search(item->stops* 1000 + item->index );
        if(item->cost > ret_cost)
        {
            continue;
        }

        if(item->index == dst)
        {
            return item->cost;
        }

        int i;
        for(i = 0; i < n; ++i)
        {
            if(graph[item->index][i] > 0 )
            {
                int tmp_cost = hash_search((item->stops + 1) * 1000 + i);
                if(graph[item->index][i] + item->cost < tmp_cost)
                {
                    struct node * n = (struct node *)malloc(sizeof(struct node ));
                    n->stops = item->stops + 1;
                    n->cost = graph[item->index][i] + item->cost;
                    n->index = i;
                    priority_queue_push(n);
                    hash_set( (item->stops + 1) * 1000 + i, n->cost);
                }
            }
        }
    }

    return -1;
}

解法2:DFS:

void dfs(int **graph, int n, int src, int dst, int K, int stops, int cost, int *ret)
{
    if( stops > K +1)
    {
        return;
    }
    if(*ret < cost)
    {
        return;
    }

    if(src == dst)
    {
        *ret = cost;
        return;
    }

    int i;
    for( i = 0; i < n; ++i)
    {
        if(graph[src][i] > 0)
        {
            dfs(graph, n, i, dst, K, stops + 1, cost + graph[src][i], ret);
        }
    }
}

int findCheapestPrice(int n, int** flights, int flightsSize, int* flightsColSize, int src, int dst, int K){

    int **graph = (int **)malloc(sizeof(int *) * n);
    int i;
    for(i = 0; i < n; ++i)
    {
        graph[i] = (int *)malloc(sizeof(int) * n);
        int j;
        for(j = 0; j < n; ++j)
        {
            graph[i][j] = 0;
        }
    }

    for(i = 0; i < flightsSize; ++i)
    {
        graph[ flights[i][0]] [ flights[i][1]] = flights[i][2];
    }

    int ret = (((unsigned int)1 << 31) -1);
   // printf("ret = %d\n", ret);
    dfs(graph, n, src, dst, K, 0, 0, &ret);
    
    if( ret == (((unsigned int)1 << 31) -1))
    {
        return -1;
    }
    else
    {
        return ret;
    }

}

3.Floyd算法:

#define MAX_COST (100 * 10000)
int findCheapestPrice(int n, int** flights, int flightsSize, int* flightsColSize, int src, int dst, int K){
    int **dist = (int **)malloc(sizeof(int *) * (K + 2));
    int i;
    for(i = 0; i < K + 2; ++i)
    {
        dist[i] = (int *)malloc(sizeof(int) * n);
        int j;
        for(j = 0; j < n; ++j)
        {
            dist[i][j] = MAX_COST;
        }
        dist[i][src] = 0;
    }


    for(i = 1; i < K + 2; ++i)
    {
        int j;
        for(j = 0; j < flightsSize; ++j)
        {
            if(dist[i][ flights[j][1] ] > dist[i-1][ flights[j][0] ] + flights[j][2])
            {
                dist[i][ flights[j][1]] = dist[i-1][ flights[j][0] ] + flights[j][2];
            }
        }
    }

    return dist[K+1][dst] == MAX_COST ? -1 : dist[K+1][dst];
}

4.改进的Flyod算法:

#define MAX_COST (100 * 10000)
int findCheapestPrice(int n, int** flights, int flightsSize, int* flightsColSize, int src, int dst, int K){
    int * dist[2];
  
    dist[0] = (int *)malloc(sizeof(int) * n);
    dist[1] = (int *)malloc(sizeof(int) * n);

    int i;
    for(i = 0; i < n; ++i)
    {
        dist[0][i] = dist[1][i] = MAX_COST;
    }
    dist[0][src] = dist[1][src] = 0;

    for(i = 0; i < K + 1; ++i)
    {
        int j;
        for(j = 0; j < flightsSize; ++j)
        {
            dist[i&1][flights[j][1]] = (dist[(~i)&1][flights[j][0]] + flights[j][2] < 
            dist[i&1][flights[j][1]]) ? (dist[(~i)&1][flights[j][0]] + flights[j][2]) :
            (dist[i&1][flights[j][1]] );
        }
    }

    return dist[K&1][dst] == MAX_COST ? -1 : dist[K&1][dst];
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值