POJ 1511 Invitation Cards (spfa||堆优化)

~~~题目链接~~~


题目大意:给出一个有向图, 现在又一些人(数量和停车站的数量相同)要到停车站发传单, 它们发完传单后又要乘公交车返回起点1,要求求他们去时和回来时的花费总额最小为多少


spfa

#include <cstdio>
#include <cstring>
using namespace std;
struct node
{
    int v, c, next;
}edge[2*1000002];
int n = 0, q = 0, cnt = 0, head1[1000002], head2[1000002], used[1000002], dist[1000002], stack[1000002];
void addedge(int u, int v, int c)
{
    edge[cnt].v = v;
    edge[cnt].c = c;
    edge[cnt].next = head1[u];
    head1[u] = cnt++;
    edge[cnt].v = u;
    edge[cnt].c = c;
    edge[cnt].next = head2[v];
    head2[v] = cnt++;
}
void spfa(int *head)
{
    int i = 0, u = 0, v = 0, c = 0, top = 0;
    memset(dist, -1, sizeof(dist));
    memset(used, 0, sizeof(used));
    memset(stack, 0, sizeof(stack));
    dist[1] = 0;
    stack[top++] = 1;
    while(top)
    {
        u = stack[--top];
        used[u] = 0;
        for(i = head[u]; i != -1; i = edge[i].next)
        {
            v = edge[i].v;
            c = edge[i].c;
            if(dist[v] == -1)
            {
                dist[v] = dist[u]+c;
                stack[top++] = v;
                used[v] = 1;
            }
            else if(dist[u]+c<dist[v])
            {
                dist[v] = dist[u]+c;
                if(!used[v])
                {
                    stack[top++] = v;
                    used[v] = 1;
                }
            }
        }
    }
}
int main()
{
    int i = 0, j = 0, t = 0, u = 0, v = 0, c = 0;
    __int64 ans = 0;
    scanf("%d", &t);
    while(t--)
    {
        cnt = 0;
        scanf("%d %d", &n, &q);
        memset(head1, -1, sizeof(head1));
        memset(head2, -1, sizeof(head2));
        for(i = 0; i<q; i++)
        {
            scanf("%d %d %d", &u, &v, &c);
            addedge(u, v, c);
        }
        ans = 0;
        spfa(head1);
        for(i = 1; i<=n; i++)
            ans += dist[i];
        spfa(head2);
        for(i = 1; i<=n; i++)
            ans += dist[i];
        printf("%I64d\n", ans);
    }
    return 0;
}

dijkstar+优先队列

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct node
{
    int v, c, next;
}edge[2*1000002];
struct qnode
{
    int v, c;
    qnode(int vv, int cc): v(vv), c(cc){}
    bool operator <(const qnode &r) const
    {
        return c>r.c;
    }
};
int n = 0, q = 0, cnt = 0, head1[1000002], head2[1000002], used[1000002], dist[1000002];

void addedge(int u, int v, int c)
{
    edge[cnt].v = v;
    edge[cnt].c = c;
    edge[cnt].next = head1[u];
    head1[u] = cnt++;
    edge[cnt].v = u;
    edge[cnt].c = c;
    edge[cnt].next = head2[v];
    head2[v] = cnt++;
}
void dijkstar(int *head)
{
    int i = 0, k = 1, p = 0,  v = 0, c = 0;
    qnode m(1, 1);
    priority_queue<qnode> q;
    memset(used, 0, sizeof(used));
    memset(dist, -1, sizeof(dist));
    used[1] = 1;
    dist[1] = 0;
    for(i = 1; i<=n; i++)
    {
        for(p = head[k]; p != -1; p = edge[p].next)
        {
            v = edge[p].v;
            c = edge[p].c;
            if(dist[v] == -1)
            {
                dist[v] = dist[k]+c;
                q.push(qnode(v, dist[v]));
            }
            else if(!used[v] && dist[k]+c<dist[v])
            {
                dist[v] = dist[k]+c;
                q.push(qnode(v, dist[v]));
            }
        }
        if(!q.empty() && used[q.top().v])
            q.pop();
        if(q.empty()) return ;
        m = q.top();
        q.pop();
        used[k = m.v] = 1;
    }
}
int main()
{
    int i = 0, t = 0, u = 0, v = 0, c = 0;
    __int64 sum = 0;
    scanf("%d", &t);
    while(t--)
    {
        cnt = 0;
        memset(head1, -1, sizeof(head1));
        memset(head2, -1, sizeof(head2));
        scanf("%d %d", &n, &q);
        for(i = 0; i<q; i++)
        {
            scanf("%d %d %d", &u, &v, &c);
            addedge(u, v, c);
        }
        sum = 0;
        dijkstar(head1);
        for(i = 1; i<=n; i++)
            sum += dist[i];
        dijkstar(head2);
        for(i = 1; i<=n; i++)
            sum += dist[i];
        printf("%I64d\n", sum);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值