现在终于知道什么是水题,模板还没理清,但一套便过。轻轻的我一飘过,这道题便AC了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#define maxn 100005
#define INF 999999999
using namespace std;
struct edge
{
int to;
int cost;
int from;
};
int V,E; //V:顶点数,E:边数。
edge es[maxn];
//vector<edge>G[maxn];
int d[maxn];
void shortest_path(int s) //传统Bellman-ford打法。
{
for(int i = 1;i <=V; i++)
d[i] = INF; //初始化
d[s] = 0;
while(true)
{
bool update = false;
for(int i = 1; i <= 2*E; i ++)
{
edge e = es[i];
//printf("%d %d %d\n",e.from, e.to, e.cost);
//printf("%d %d \n",d[e.to],d[e.from]);
if(d[e.from] != INF && d[e.to] > d[e.from]+e.cost)
{
d[e.to] = d[e.from]+e.cost;
//printf("%d\n",e.cost);
update = true;
}
}
if(!update)break;
}
}
int main()
{
int i, a,j;
int T, S, D;
int min1;
int p[1000],q[100000] ;
edge b;
while(~scanf("%d%d%d",&T,&S,&D))
{
//if(V==0&&E==0)break;
for(i= 1,V = 0;i <= 2*T; i++)
{
scanf("%d%d%d",&es[i].from, &es[i].to, &es[i].cost);
i++;
es[i].from = es[i-1].to;
es[i].to = es[i-1].from;
es[i].cost = es[i-1].cost;
if(V<es[i].from)V = es[i].from;
if(V<es[i].to)V = es[i].to;
//G[a].push_back(b);
//printf("%d %d",b.to,b.cost);
}
E = T ;
for(i = 1; i <= S; i ++)
scanf("%d",&p[i]);
for(i = 1; i<= D; i++)
scanf("%d", &q[i]);
for(i = 1,min1 = INF; i <= S; i ++) //启动多重单源最短路模式。。此题比较水。。居然没超时。。
{
shortest_path(p[i]);
for(j = 1; j <= D; j ++)
{
if( d[q[j]] < min1)min1 = d[q[j]];
}
}
printf("%d\n", min1);
}
return 0;
}