POJ 1511-Invitation Cards(SPFA)

版权声明:转载请标明出处哦 https://blog.csdn.net/PleasantlY1/article/details/82777543

Description

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

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

All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers 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 of money to pay every day for the transport of their employees.

Input

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. 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 the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.

Output

For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel 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

题目大意:一群人要干什么,然后在每个公交车站留人,有源点,求来回坐车最小花费。

思路:最近做最短路感觉做题不能被模板约束了,也不要局限于模板,要学会他的思想与原理,才能做到真正的掌握这个算法。

这个题很裸,求是所有点到源点的最短路之和。直接上板子就能做,spfa或者dij。用来试了一下spfa用栈来写,结果发现自己对spfa的理解好像有点问题有扯到差分约束系统上去了,写了后发现用栈与用队列效率上没什么差别,但是在空间上有区别,此处考虑栈与队列的性质。(2次SPFA跑出所有点的最短路累加)

还没有试过spfa加前向星与LLL + SLF优化。遇到卡的再试呢还是抽空试呢,这是个问题.....

代码如下:

#include<set>
#include<map>
#include<list>
#include<deque>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<stdio.h>
#include<sstream>
#include<stdlib.h>
#include<string.h>
//#include<ext/rope>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
#define per(i,a,b) for(int i=a;i<=b;++i)
#define LL long long 
#define swap(a,b) {int t=a;a=b;b=t} 
using namespace std;
//using namespace __gnu_cxx;
#define N 1000010
LL d[N];
int n,m;
int head[N];
int vis[N];
struct node{
    int u;
	int v;
	int w;
	int next;
}p[N],t[N];
void add(int u,int v,int w,int k)
{
    p[k].u=u;
    p[k].v=v;
    p[k].w=w;//权 
    p[k].next=head[u];//出发点 
    head[u]=k;
}
LL spfa_stack()//先进先出 
{
    stack<int>q;
    memset(d,INF,sizeof(d));
    memset(vis,0,sizeof(vis));
    while(!q.empty()) q.pop();
    d[1]=0;
    vis[1]=1;
    q.push(1);
    while(!q.empty())
    {
        int u=q.top();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=p[i].next)//类似链表实现遍历该点的所有边 
        {
            int v=p[i].v;
            int w=p[i].w;
            if(d[u]+w<d[v]&&vis[v]==0)
            {
                d[v]=d[u]+w;
                q.push(v);
            }
        }
    }
    LL s=0;
    per(i,1,n) s+=d[i];
    return s;
}
int main()
{
    int T,a,b,c;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(head,-1,sizeof(head));
        per(i,0,m-1)
        {
            scanf("%d%d%d",&a,&b,&c);
            t[i].u=a;
			t[i].v=b;
			t[i].w=c;
            add(a,b,c,i);
        }
        LL s1=spfa_stack();
        memset(head,-1,sizeof(head));
        per(i,0,m-1) add(t[i].v,t[i].u,t[i].w,i);
        LL s2=spfa_stack();
        printf("%lld\n",s1+s2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/PleasantlY1/article/details/82777543