蒟蒻求助,70pts
查看原帖
蒟蒻求助,70pts
476574
KAWorld楼主2021/2/3 10:17
#include<bits/stdc++.h>
using namespace std;
struct node
{
	int start,reach,weight,cost,next;
}edge[5005];
struct again
{
	int pos,dis;
	friend bool operator<(again a,again b)
	{
		return a.dis>b.dis;
	}
};
int n,m,tot=0,head[105],dis[105];
bool f[105];
void add(int u,int v,int w,int t)
{
	edge[tot].start=u;edge[tot].reach=v;
	edge[tot].weight=w;edge[tot].cost=t;
	edge[tot].next=head[u];
	head[u]=tot;
}
void dijkstra(int begin)
{
	priority_queue<again>que;
	again temp;
	for (int i=1;i<=n;++i) dis[i]=2147483647;
	temp.pos=begin;temp.dis=0;dis[begin]=0;f[begin]=true;
	que.push(temp);
	while (!que.empty())
	{
		again tmp=que.top();
		que.pop();
		for (int i=head[tmp.pos];i!=0;i=edge[i].next)
			if (f[edge[i].reach]==false&&dis[tmp.pos]+edge[i].cost<dis[edge[i].reach])
			{
				f[edge[i].reach]=true;
				dis[edge[i].reach]=dis[tmp.pos]+edge[i].cost;
				again t;
				t.pos=edge[i].reach;t.dis=dis[edge[i].reach];
				que.push(t);
			}
	}
}
int main()
{
	int a,b,l,d,x,y;
	cin>>n>>m;
	for (int i=1;i<=m;++i)
	{
		cin>>a>>b>>l;
		++tot;add(a,b,l,0);
		++tot;add(b,a,l,0);
	}
	cin>>d;
	for (int i=1;i<=d;++i)
	{
		cin>>x>>y;
		for (int j=head[x];j!=0;j=edge[j].next)
			if (edge[j].reach==y)
			{
				edge[j].cost=edge[j].weight;break;
			}
		for (int j=head[y];j!=0;j=edge[j].next)
			if (edge[j].reach==x)
			{
				edge[j].cost=edge[j].weight;break;
			}
	}
	cin>>x>>y;
	dijkstra(x);
	cout<<dis[y];
	return 0;
}

2021/2/3 10:17
加载中...