UVa 10986 - Sending email

203 篇文章 0 订阅
36 篇文章 0 订阅

題目:發送郵件,求最短路徑。

分析:圖論,最短路。直接利用dijkstra算法+優先隊列優化。

說明:spfa+stack超時了╮(╯▽╰)╭。

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

//banery_heap
int heap_keys[20002];
int heap_base[20002];
int heap_in_h[20002];
int heap_size;

void heap_ini()
{
	heap_size = 0;
	memset(heap_keys, 0, sizeof(heap_keys));
	memset(heap_base, 0, sizeof(heap_base));
	memset(heap_in_h, 0, sizeof(heap_in_h));
}

void heap_insert(int ID, int Key)
{
	if (!heap_in_h[ID]) {
		heap_in_h[ID] = ++ heap_size;
		heap_base[heap_size] = ID;
	}
	heap_keys[heap_in_h[ID]] = Key;
	int now = heap_in_h[ID];
	while (now > 1 && heap_keys[now] < heap_keys[now>>1]) {
		swap(heap_base[now], heap_base[now>>1]);
		swap(heap_keys[now], heap_keys[now>>1]);
		swap(heap_in_h[heap_base[now]], heap_in_h[heap_base[now>>1]]);
		now = now>>1;
	}
}

int heap_delete()
{
	swap(heap_base[heap_size], heap_base[1]);
	swap(heap_keys[heap_size], heap_keys[1]);
	swap(heap_in_h[heap_base[heap_size]], heap_in_h[heap_base[1]]);
	int now = 1;
	while (1) {
		int New = now, L = (now<<1), R = (now<<1)+1;
		if (L < heap_size && heap_keys[L] < heap_keys[New]) New = L;
		if (R < heap_size && heap_keys[R] < heap_keys[New]) New = R;
		if (now == New ) break;
		swap(heap_base[now], heap_base[New]);
		swap(heap_keys[now], heap_keys[New]);
		swap(heap_in_h[heap_base[now]], heap_in_h[heap_base[New]]);
		now = New;
	}
	return heap_base[heap_size --];
}
//banery_heap end

//link_list
typedef struct _link_list
{
	int point;
	int path;
	_link_list *next;
}link_list;

link_list *link_head[20002];
link_list  link_node[100001];
int link_size;

void link_ini()
{
	link_size = 0;
	memset(link_head, 0, sizeof(link_head));
}

void link_add(int s, int t, int p)
{
	link_node[link_size].point = t;
	link_node[link_size].path = p;
	link_node[link_size].next = link_head[s];
	link_head[s] = &link_node[link_size ++];
}
//link_list end

int  dist[20002];
int  stack[20002];
int  visit[20002];
void dijkstra(int n, int s, int t)
{
	int oo = 1000000000;
	for (int i = 0; i < n; ++ i) {
		dist[i] = oo;
		visit[i] = 0;
	}
	
	dist[s] = 0;
	heap_insert(s, 0);
	visit[s] = 1;
	
	while (heap_size > 0) {
		int now = heap_delete();
		for (link_list *p = link_head[now]; p; p = p->next) {
			if (dist[p->point] > dist[now] + p->path) {
				visit[p->point] = 1;
				dist[p->point] = dist[now] + p->path;
				heap_insert(p->point, dist[p->point]);
			}
		}
	}
	
	if (dist[t] == oo)
		printf("unreachable\n");
	else printf("%d\n",dist[t]);
}

int main()
{
	int c,s,t,n,m,u,v,p;
	while (~scanf("%d",&c)) 
	for (int k = 1; k <= c; ++ k) {
		scanf("%d%d%d%d",&n,&m,&s,&t);
		link_ini();
		heap_ini();
		for (int i = 0; i < m; ++ i) {
			scanf("%d%d%d",&v,&u,&p);
			link_add(u, v, p);
			link_add(v, u, p);
		}
		
		printf("Case #%d: ",k);
		dijkstra(n, s, t);
	}
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值