hdu1599

/*
分析:
    悲催的读错题而WA了昨天一下午,囧~,起点不一定
是点1,囧~~~
    第一个是Dij朴素拆边的,400+MS;后一个是floyd求
最小环的,100+MS。最后再一次,囧~~~


                                          2012-10-15
*/








#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"queue"
using namespace std;
int n;
int map[111][111];
struct node
{
	int index;
	int dis;
	friend bool operator<(node n1,node n2)
	{
		return n1.dis>n2.dis;
	}
};
int Dij(int s,int e)
{
	int l;
	int flag[111],dis[111];
	priority_queue<node>q;
	node now,next;

	memset(dis,127,sizeof(dis));
	dis[s]=0;
	memset(flag,0,sizeof(flag));
	now.index=s;
	now.dis=0;
	q.push(now);

	while(!q.empty())
	{
		now=q.top();
		q.pop();
		if(flag[now.index])	continue;
		flag[now.index]=1;
		if(dis[now.index]>dis[e])	break;
		for(l=1;l<=n;l++)
		{
			if(dis[now.index]+map[now.index][l]<dis[l])
			{
				dis[l]=dis[now.index]+map[now.index][l];
				next.index=l;
				next.dis=dis[l];
				q.push(next);
			}
		}
	}
	return dis[e];
}
int main()
{
	int m;
	int i,l;
	int a,b,c;
	int t,temp;
	int ans;
	while(scanf("%d%d",&n,&m)!=-1)
	{
		memset(map,127,sizeof(map));
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&c);
			map[a][b]=map[b][a]=map[a][b]>c?c:map[a][b];
		}

		ans=2139062143;
		for(i=1;i<=n;i++)
		{
			for(l=i+1;l<=n;l++)
			{
				if(i==l)	continue;
				if(map[i][l]!=2139062143)
				{
					temp=map[i][l];
					map[i][l]=map[l][i]=2139062143;
					t=Dij(l,i);
					t+=temp;
					ans=ans>t?t:ans;
					map[i][l]=map[l][i]=temp;
				}
			}
		}
		if(ans==2139062143)	printf("It's impossible.\n");
		else				printf("%d\n",ans);
	}
	return 0;
}




#include"stdio.h"
#include"string.h"
#include"stdlib.h"
int map[111][111];
int dis[111][111];
int main()
{
	int n,m;
	int k,i,l;
	int a,b,c;
	int temp,ans;
	while(scanf("%d%d",&n,&m)!=-1)
	{
		for(i=1;i<=n;i++)
		for(l=1;l<=n;l++)
			map[i][l]=10000000;
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&c);
			if(a==b)	continue;
			map[a][b]=map[b][a]=map[a][b]>c?c:map[a][b];
		}
		for(i=1;i<=n;i++)
		for(l=1;l<=n;l++)
			dis[i][l]=map[i][l];

		ans=10000000;
		for(k=1;k<=n;k++)
		for(i=1;i<=n;i++)
		for(l=1;l<=n;l++)
		{
			if(i==l)	continue;
			temp=dis[i][l]+map[i][k]+map[k][l];
			ans=ans>temp?temp:ans;
			temp=dis[i][k]+dis[k][l];
			dis[i][l]=dis[i][l]>temp?temp:dis[i][l];
		}
		if(ans==10000000)	printf("It's impossible.\n");
		else				printf("%d\n",ans);
	}
	return 0;
}


你可能感兴趣的:(hdu1599)