2013寒假練習 1026 Til the Cows Come Home

地址:http://acm.bit.edu.cn/mod/programming/view.php?id=677

裸的dijkstra。。今次使用鄰接表

#include<iostream>
#define INF 0x7f7f7f7f
typedef struct NODE
{
	int v,l;
	struct NODE * next;
}node;
node map[1005],*temp;
bool flag[1005];
int dis[1005];
int main()
{
	int t,n,T1,T2,T3,i,j;
	scanf("%d%d",&t,&n);
	memset(flag,false,sizeof(flag));
	memset(dis,0x7f,sizeof(dis));
	dis[1]=0;
	memset(map,0,sizeof(map));
	while(t--)
	{
		scanf("%d%d%d",&T1,&T2,&T3);
		temp=(node *)malloc(sizeof(node));
		temp->next=map[T1].next,temp->l=T3,temp->v=T2;
		map[T1].next=temp;
		temp=(node *)malloc(sizeof(node));
		temp->next=map[T2].next,temp->l=T3,temp->v=T1;
		map[T2].next=temp;
	}
	for(i=1;i<=n;i++)
	{
		int mint=-1;
		for(j=1;j<=n;j++)
		{
			if(!flag[j])
			{
				if(mint==-1||dis[j]<dis[mint]) mint=j;
			}
		}
		flag[mint]=true;
		for(temp=&map[mint];temp;temp=temp->next)
		{
			if(dis[temp->v]>dis[mint]+temp->l) dis[temp->v]=dis[mint]+temp->l;
		}
	}
	printf("%d\n",dis[n]);
	return 0;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章