方法:建立超级源点和汇点
HDU -2680 Choose the best route
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <bitset>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
const int maxn = 2e5+5;
struct Edge{
int to,next,val;
}edge[maxn];
int m,n,s;
int head[maxn],tot,dis[maxn];
bool vis[maxn];
struct node{
int dis,pos;
bool operator<(const node &x)const {
return x.dis<dis;
}
};
void add_edge(int u,int v,int w){
edge[tot].to=v;
edge[tot].next=head[u];
edge[tot].val=w;
head[u]=tot++;
}
void Diskrea(int st)
{
memset(vis,false,sizeof(vis));
memset(dis,INF,sizeof(dis));
priority_queue<node>q;
q.push({0,st});
dis[st]=0;
while (!q.empty()){
node tmp=q.top();q.pop();
int x=tmp.pos;
if(vis[x]) continue; vis[x]=true;
for(int i=head[x];i!=-1;i=edge[i].next){
int y=edge[i].to;
if(dis[y]>dis[x]+edge[i].val){
dis[y]=dis[x]+edge[i].val;
if(!vis[y]) q.push({dis[y],y});
}
}
}
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&s))
{
tot=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=m;i++){
int u=read(),v=read(),w=read();
add_edge(u,v,w);
}
int q=read();
for(int i=1;i<=q;i++){
int v=read();
add_edge(0,v,0);
}
Diskrea(0);
if(dis[s]==INF) puts("-1");
else printf("%d\n",dis[s]);
}
return 0;
}
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <bitset>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;}
const int maxn = 1e5+5;
int t,s,d;
int head[maxn],tot,dis[maxn];
bool vis[maxn];
struct Edge{
int to,next,val;
}edge[maxn];
struct node{
int dis,pos;
bool operator<(const node &x)const{
return x.dis<dis;
}
};
void add_edge(int u,int v,int w){
edge[tot].to=v;
edge[tot].next=head[u];
edge[tot].val=w;
head[u]=tot++;
}
void Dijskra(int st)
{
memset(dis,INF,sizeof(dis));
memset(vis,false,sizeof(vis));
priority_queue<node>q;
dis[st]=0;
q.push({0,st});
while (!q.empty())
{
int x=q.top().pos; q.pop();
if(vis[x]) continue; vis[x]=true;
for(int i=head[x];i!=-1;i=edge[i].next){
int y=edge[i].to;
if(dis[y]>dis[x]+edge[i].val){
dis[y]=dis[x]+edge[i].val;
if(!vis[y]) q.push({dis[y],y});
}
}
}
}
int main()
{
while (~scanf("%d%d%d",&t,&s,&d))
{
tot=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=t;i++){
int u=read(),v=read(),w=read();
add_edge(u,v,w);
add_edge(v,u,w);
}
for(int i=1;i<=s;i++){
int pos=read();
add_edge(0,pos,0);
add_edge(pos,0,0);
}
int fin=10005;
for(int i=1;i<=d;i++){
int ed=read();
add_edge(ed,fin,0);
add_edge(fin,ed,0);
}
Dijskra(0);
printf("%d\n",dis[fin]);
}
return 0;
}