poj 1511 spfa入门

原创 2016年05月30日 20:21:12

Invitation Cards

Time Limit: 8000MS


Memory Limit: 262144K

Total Submissions: 24003


Accepted: 7918

Description

In the age of television, not manypeople attend theater performances. Antique Comedians of Malidinesia are awareof this fact. They want to propagate theater and, most of all, AntiqueComedies. They have printed invitation cards with all the necessary informationand with the programme. A lot of students were hired to distribute theseinvitations among the people. Each student volunteer has assigned exactly onebus stop and he or she stays there the whole day and gives invitation to peopletravelling by bus. A special course was taken where students learned how toinfluence people and what is the difference between influencing and robbery.

The transport system is very special: all lines are unidirectional and connectexactly two stops. Buses leave the originating stop with passangers each halfan hour. After reaching the destination stop they return empty to theoriginating stop, where they wait until the next full half an hour, e.g. X:00or X:30, where 'X' denotes the hour. The fee for transport between two stops isgiven by special tables and is payable on the spot. The lines are planned insuch a way, that each round trip (i.e. a journey starting and finishing at thesame stop) passes through a Central Checkpoint Stop (CCS) where each passengerhas to pass a thorough check including body scan.

All the ACM student members leave the CCS each morning. Each volunteer is tomove to one predetermined stop to invite passengers. There are as manyvolunteers as stops. At the end of the day, all students travel back to CCS.You are to write a computer program that helps ACM to minimize the amount ofmoney to pay every day for the transport of their employees.

Input

The input consists of N cases. Thefirst line of the input contains only positive integer N. Then follow thecases. Each case begins with a line containing exactly two integers P and Q, 1<= P,Q <= 1000000. P is the number of stops including CCS and Q thenumber of bus lines. Then there are Q lines, each describing one bus line. Eachof the lines contains exactly three numbers - the originating stop, thedestination stop and the price. The CCS is designated by number 1. Prices arepositive integers the sum of which is smaller than 1000000000. You can alsoassume it is always possible to get from any stop to any other stop.

Output

For each case, print one linecontaining the minimum amount of money to be paid each day by ACM for thetravel costs of its volunteers.

Sample Input

2

2 2

1 2 13

2 1 33

4 6

1 2 10

2 1 60

1 3 20

3 4 10

2 4 5

4 1 50

Sample Output

46

210

Source

CentralEurope 1998

 

 

正着找一遍最短路 倒着再找一遍 就可以了

直接套用spfa的模板

 

AC代码:

#include<iostream>

#include<cstdio>

#include<memory.h>

#include<queue>

usingnamespace std;

#definemax(x,y) (x>y?x:y)

#definemin(x,y) (x<y?x:y)

#defineINF 0x3f3f3f3f

#defineMAX1 1000005

#defineMAX2 1000005

structnode

{

    int u,v;

    long long c;

    node(){}

        node(int u,int v,long long c):u(u),v(v),c(c){}

};

nodep[MAX2];

inthead[MAX1],next1[MAX2];

longlong dp[MAX1];

boolmap[MAX1];

intcnt[MAX1];

inte,top,n,m;

voidaddnode(int u,int v,long long c)

{

    p[e] = node(u,v,c);

    next1[e] = head[u];

    head[u] = e++;

}

voidinit()

{

    memset(head, -1, sizeof(head[0])*(n + 2));

    memset(next1, -1, sizeof(next1[0])*(n +2));

    e = 0;

    for(int i = 0; i < m; i++)

    {

        int u,v;

        long long c;

       scanf("%d%d%I64d",&u,&v,&c);

        addnode(u,v,c);

    }

}

voidun_init()

{

    memset(head,-1,sizeof(head[0])*(n + 2));

    memset(next1,-1,sizeof(next1[0])*(n + 2));

    e = 0;

    for(int i = 0; i < m; i++)

    {

        addnode(p[i].v, p[i].u, p[i].c);

    }

}

longlong get_sum(long long a[])

{

    long long res = 0;

    for(int i = 1; i <= n; i++)

        res+=a[i];

    return res;

}

boolrelax(int u,int v,long long c)

{

    if(dp[v] > dp[u] + c)

    {

        dp[v] = dp[u] + c;

        return true;

    }

    return false;

}

voidspfa(int t)

{

    memset(map,0,sizeof(map[0])*(n + 2));

    for(int i = 1; i <= n; i++)

        dp[i] = INF;

    dp[t] = 0;

    map[t] = true;

    top = 0;

    cnt[top++] = t;

    while(top)

    {

        int pre = cnt[--top];

        map[pre] = false;

        for(int j = head[pre]; j + 1; j =next1[j])

        {

            if(relax(p[j].u, p[j].v,p[j].c)&&map[p[j].v] == false)

            {

                cnt[top++] = p[j].v;

                map[p[j].v] = true;

            }

        }

    }

}

intmain()

{

    int t;

    scanf("%d",&t);

    while(t--)

    {

        scanf("%d%d",&n,&m);

        init();

        spfa(1);

        long long ans = get_sum(dp);

        un_init();

        spfa(1);

        ans += get_sum(dp);

        printf("%I64d\n",ans);

    }

    return 0;

}


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ 1511(邻接表和最短路 SPFA)

Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 25052 Acc...

POJ 1511 Invitation Cards(SPFA)

POJ 1511 Invitation Cards(SPFA) http://poj.org/problem?id=1511 题意:有一个N个点的有向图,点的编号从1到N.现在要你求从1号点到所有其他...

POJ-1511-Invitation Cards(SPFA 反向建图)

Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 25160 Acc...

POJ 1511 Invitation Cards 【最短路,spfa算法,Dijkstra算法堆优化】

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 25219   Accepte...

poj 1511&&zoj 2008 Invitation Cards 最短路+SPFA

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 19250...

Poj 1511 Invitation Cards -- spfa

/* 方法:spfa算法。注意在存边的时候,此题在数据上卡掉了vector,可以用邻接表。 因为本题要计算人去发传单和回来的最小花费之和,所以需要两次spfa。 */ #include #...

最短路练习10/poj/1511 Invitation Cards ,(两次spfa),(单源最短路,优先队列优化的Dijkstra)

题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS   Memory Lim...

SPFA邻接表-POJ-1511-Invitation Cards

Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 22675 Acc...

poj 1511 spfa 模板题

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 26478   Accepte...

POJ-1511 Invitation Cards (SPFA算法+邻接表)

题目大意: 这道题和我以前做过的一道题很像,大概就是说,求得源点到各个点的最短路径相加,和各个点到源点的最短路径相加之和的和。源点到各点的最短路径其实很好求,各点到源点的最短路径其实就是将矩阵翻一下...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)