最短路:单源多汇,多源多汇


方法:建立超级源点和汇点


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;
}

HDU - 2066 一个人的旅行

#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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值