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;

}


 

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

邻接表和SPFA(POJ 1511)

通常我们在表示图的时候,常用的是邻接矩阵和邻接表。邻接矩阵可以表示图中任意两点之间的距离。而邻接表则是对于每一个节点建立一个链表,包含的是关联这个节点的任意一条边(可能是无向图,也可能是有向图)。我们...
  • aaron_1996
  • aaron_1996
  • 2015年06月12日 17:02
  • 276

poj 2516 Minimum(SPFA解决最小费用最大流)

Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 16007   Accepted: 56...
  • a15110103117
  • a15110103117
  • 2016年09月26日 19:10
  • 136

hdu 2680 最短路基础题 spfa实现 算法总结

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意,就是一个人可以从多个起点开始出发,看到终点的最短路是多少。。只有可以运用和hdu2...
  • liujc_
  • liujc_
  • 2015年04月13日 09:00
  • 610

【基础练习】【SPFA】codevs1557 热浪题解

题目描述 Description 德克萨斯纯朴的民眾们这个夏天正在遭受巨大的热浪!!!他们的德克萨斯长角牛吃起来不错,可是他们并不是很擅长生產富含奶油的乳製品。Farmer John此时以先天下之忧...
  • ametake
  • ametake
  • 2015年10月10日 15:48
  • 773

POJ 1511 spfa求最短回路

http://poj.org/problem?id=1511 题意:(图论147)。求从起点到各个点的最短回路。 分析:用两次spfa就行了。第一次spfa是从出发点start到各个点,求一次最短...
  • lanjiangzhou
  • lanjiangzhou
  • 2013年06月14日 19:47
  • 394

POJ 1511 链式前向星+SPFA

#include #include #include using namespace std; const int maxn=1000010,inf=1000000000; long long ans...
  • huangkuizuiniu
  • huangkuizuiniu
  • 2016年02月01日 15:18
  • 11748

POJ 3259 Wormholes(SPFA算法判断是否存在负环)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36755   Accepted: 13457...
  • zwj1452267376
  • zwj1452267376
  • 2015年08月19日 21:03
  • 749

【转载】SPFA最短路算法解析

SPFA是一个比较高效的算法,虽说在比较大的数据时仍然无法比Dijkestra+Heap快,但是代码却很简单,以我的水平而言,代码要短上一倍以上。SPFA这个算法的实用性比较强,所以在OI中是一个相当...
  • Dream_Lolita
  • Dream_Lolita
  • 2016年08月08日 09:32
  • 223

poj 初期基本搜索

第三个专题了,初期基本搜索: 都是水题,两天完全可以刷完。。。 (1)、深度优先搜索 1、poj2488 题意:给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的...
  • consciousman
  • consciousman
  • 2017年01月19日 10:37
  • 568

透析SPFA算法(图例讲解)

SPFA算法是Bellman-Ford的队列优化,所以先介绍Bellman-Ford算法。        Dijkstra算法是处理单源最短路径的有效算法,但它局限于边的权值非负的情况,若图中出现权值...
  • u013445530
  • u013445530
  • 2014年12月05日 23:53
  • 1497
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 1511 spfa入门
举报原因:
原因补充:

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