题目链接:CF 707B
多起点多终点求其中与终点相连的所有最短路中的最短长度,感觉跟优先队列有关,然后设一开始的所有面粉点即不能达到的d为0,其他都设为INF,把所有的面粉点压入队列进行BFS,最后选出1~n点中不为0的最短距离……一开始没考虑重边而且面粉店的d设的不对,WA两发……看了下其他人写的好像直接暴力比较(因为最短肯定是直接相连)而且速度跟BFS一样,我的做法比较麻烦……
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100010;
struct info
{
int to;
int pre;
LL dx;
};
struct A
{
int cur;
LL dx;
bool operator<(const A &t)const
{
return dx>t.dx;
}
};
A pos[N];
info E[N<<1];
int head[N<<1],ne;
LL d[N];
priority_queue<A>Q;
void init()
{
CLR(head,-1);
ne=0;
CLR(d,INF);
while (!Q.empty())
Q.pop();
}
void add(int s,int t,LL dx)
{
E[ne].to=t;
E[ne].dx=dx;
E[ne].pre=head[s];
head[s]=ne++;
}
void bfs(int k)
{
int i;
for (i=0; i<k; ++i)
{
Q.push(pos[i]);
d[pos[i].cur]=0;
}
while (!Q.empty())
{
A now=Q.top();
Q.pop();
for (i=head[now.cur]; ~i; i=E[i].pre)
{
A v=now;
v.cur=E[i].to;
v.dx+=E[i].dx;
if(d[v.cur]>v.dx)
{
d[v.cur]=v.dx;
Q.push(v);
}
}
}
}
int main(void)
{
int n,m,k,i,j,a,b,temp;
LL dx;
while (~scanf("%d%d%d",&n,&m,&k))
{
init();
for (i=0; i<m; ++i)
{
scanf("%d%d%I64d",&a,&b,&dx);
add(a,b,dx);
add(b,a,dx);
}
for (i=0; i<k; ++i)
{
scanf("%d",&temp);
pos[i].cur=temp;
pos[i].dx=0;
}
bfs(k);
LL ans=d[0];
for (i=1; i<=n; ++i)
{
if(d[i]&&d[i]<ans)
ans=d[i];
}
printf("%I64d\n",ans!=d[0]?ans:-1LL);
}
return 0;
}