HDU - 1535 - Invitation Cards

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1535

题目就是问你从1点出发到各个点再从各个点回来最少要花多少钱之类的,就是单源最短路的问题,因为是有向图,所以a - b和b - a的花费是不同的,不过只要把图反一下就行了,然后就是求两次最短路的问题,spfa和Dijkstra都可以的,因为数据有点大,所以用邻接表来存图。

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<queue>
using namespace std;
const int maxn = 1001000,inf = 1000000100;
int t,n,m,head[2][maxn],cost[maxn],vis[maxn];
struct node
{
    int to;
    int cost;
    int next;
}e[2][maxn];
void dijkstra(int x)
{
    int st = 1;
    while(1)
    {
        vis[st] = 1;
        int pos = head[x][st];
        while(pos!=-1)
        {
            node a = e[x][pos];
            if(cost[a.to] > cost[st] + a.cost)
                cost[a.to] = cost[st] + a.cost;
            pos = a.next;
        }
        int mi = inf;
        pos = -1;
        for(int i=1;i<=n;i++)
        {
            if(vis[i])
                continue;
            if(mi > cost[i])
                mi = cost[i],pos = i;
        }
        if(pos == -1)
            break;
        st = pos;
    }
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset(e,-1,sizeof e);
        memset(head,-1,sizeof head);
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            int a,b,d;
            scanf("%d%d%d",&a,&b,&d);
            if(head[0][a]==-1)
            {
                head[0][a] = i;
                e[0][i].to = b;
                e[0][i].cost = d;
            }
            else
            {
                int next = e[0][head[0][a]].next;
                e[0][head[0][a]].next = i;
                e[0][i].to = b;
                e[0][i].cost = d;
                e[0][i].next = next;
            }
            if(head[1][b]==-1)
            {
                head[1][b] = i;
                e[1][i].to = a;
                e[1][i].cost = d;
            }
            else
            {
                int next = e[1][head[1][b]].next;
                e[1][head[1][b]].next = i;
                e[1][i].to = a;
                e[1][i].cost = d;
                e[1][i].next = next;
            }
        }
        for(int i=0;i<=n;i++)
            cost[i] = inf;
        cost[1] = 0;
        memset(vis,0,sizeof vis);
        //spfa(0);
        dijkstra(0);
        //dij_up(0);
        long long ans = 0;
        for(int i=1;i<=n;i++)
            ans += cost[i];
        for(int i=0;i<=n;i++)
            cost[i] = inf;
        cost[1] = 0;
        memset(vis,0,sizeof vis);
        //spfa(1);
        dijkstra(1);
        //dij_up(1);
        for(int i=1;i<=n;i++)
            ans += cost[i];
        printf("%lld\n",ans);
    }
    return 0;
}
struct point
{
    int cost ;
    int num;
    friend bool operator < (point a,point b)
    {
        return a.cost > b.cost;
    }
};
void dij_up(int x)
{
    priority_queue<point>q;
    point st;
    st.cost = 0;
    st.num = 1;
    q.push(st);
    while(!q.empty())
    {
        st = q.top();
        q.pop();
        if(st.cost>cost[st.num])
            continue;
        int pos = head[x][st.num];
        int start = st.num;
        while(pos!=-1)
        {
            node a = e[x][pos];
            if(cost[a.to] > cost[start] + a.cost)
            {
                cost[a.to] = cost[start] + a.cost;
                st.num = a.to;
                st.cost = cost[a.to];
                q.push(st);
            }
            pos = a.next;
        }
    }
}
void spfa(int x)
{
    queue<int>q;
    q.push(1);
    while(!q.empty())
    {
        int st = q.front();
        q.pop();
        vis[st] = 0;
        int pos = head[x][st];
        while(pos!=-1)
        {
            node next_e = e[x][pos];
            if(cost[st]+next_e.cost < cost[next_e.to])
            {
                cost[next_e.to] = cost[st]+next_e.cost;
                if(vis[next_e.to] == 0)
                {
                    q.push(next_e.to);
                    vis[next_e.to] = 1;
                }
            }
            pos = next_e.next;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值