板子板子,学长给的
#include
using namespace std;
const int N=1100;
const int inf=0x3f3f3f3f;
bool vis[N];
int dis[N];
int n;
vector >mp[N];
struct node
{
int id;//点
int len;//边权
friend bool operator <( node A,node B)
{
return A.len>B.len;
}
};
void Dijkstra(int s)
{
memset(dis,inf,sizeof(dis));
memset(vis,false,sizeof(vis));
dis[s]=0;
priority_queueQ;
Q.push(node{s,0});
while(!Q.empty())
{
node u=Q.top();
Q.pop();
int xx=u.id;
if(vis[xx])
continue;
vis[xx]=true;
for(int i=0; idis[xx]+mp[xx][i].second)
{
dis[to]=dis[xx]+mp[xx][i].second;
Q.push(node{to,dis[to]});
}
}
}
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=0; i>a>>b>>c;
mp[a].push_back(make_pair(b,c));
mp[b].push_back(make_pair(a,c));
}
Dijkstra(1);
for(int i=1; i<=n; i++)
cout<<"S到序号"<
据说下面这个功能优化一点
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int N=1e3+100;
typedef long long ll;
struct node
{
long long u,w;
node(long long _u=0,long long _w=0):u(_u),w(_w){};
bool operator <(const node &a) const
{
return w>a.w;
}
};
struct Edge
{
ll to,w,nextt;
}A[1010000];
ll head[N],vis[N],dis[N];
ll tot;
priority_queueq;
void addedge(ll from,ll to,ll w)
{
A[tot].to=to;
A[tot].w=w;
A[tot].nextt=head[from];
head[from]=tot++;
}
void Dijkstra(ll start)
{
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[start]=0;
while(!q.empty()) q.pop();
q.push(node(start,0));
node tem;
while(!q.empty())
{
tem=q.top();
q.pop();
long long u=tem.u;
if(vis[u]) continue;
vis[u]=1;
for(long long i=head[u];i!=-1;i=A[i].nextt)
{
long long v=A[i].to;
long long w=A[i].w;
if(!vis[v]&&dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
q.push(node(v,dis[v]));
}
}
}
}
void init()
{
memset(head,-1,sizeof(head));
tot=0;
}
ll sovle1(int n)
{
Dijkstra(0);
ll cc=0;
for(long long i=1;i<=n;i++) cc=max(cc,dis[i]);
return cc;
}
ll solve2(int s,int n)
{
Dijkstra(s);
ll cc=0;
for(long long i=1;i<=n;i++) cc=max(cc,dis[i]);
return cc;
}
int main()
{
int T;scanf("%d",&T);
while(T--){
init();
ll n,m,s,k,c,t,u,v,w,x;
scanf("%lld%lld%lld%lld%lld",&n,&m,&s,&k,&c);
for(long long i=1;i<=k;i++){
scanf("%lld",&x);
addedge(0,x,0);
}
for(long long i=1;i<=m;i++)
{
scanf("%lld%lld%lld",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
ll t1=sovle1(n);
ll t2=solve2(s,n);
printf("%lld\n",t1*c>=t2?t2:t1);
}
}