P1339 [USACO09OCT]热浪Heat Wave

#include
using namespace std;
int n,m,s,e,head[2550],edgenum,in[2555],dis[2555];
struct Node
{
	int v,w,next;
}edge[6220*2];
void addedge(int u,int v,int w)
{
	edge[++edgenum].v=v;
	edge[edgenum].w=w;
	edge[edgenum].next=head[u];
	head[u]=edgenum;
}
bool spfa(int h)
{
	memset(dis,0x3f3f3f3f,sizeof(dis));
	memset(in,0,sizeof(in));
	in[h]=1;
	dis[h]=0;
	queue<int> q;
	q.push(h);
	while(q.size())
	{
		int tem=q.front();
		q.pop();
		in[tem]=0;
		for(int i=head[tem];i;i=edge[i].next)
		{
			int v=edge[i].v,w=edge[i].w;
			if(dis[v]>dis[tem]+w)
			{
				dis[v]=dis[tem]+w;
				if(!in[v]) q.push(v),in[v]=1;
			}
		}
	}
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>m>>s>>e;
	for(int i=0;i<m;i++)
	{
		int u,v,w;
		cin>>u>>v>>w;
		addedge(u,v,w);
		addedge(v,u,w);
	}
	spfa(s);
	cout<<dis[e]<<endl;
	return 0;
}

你可能感兴趣的:(P1339 [USACO09OCT]热浪Heat Wave)