首页 > ACM题库 > HDU-杭电 > HDU 1535 Invitation Cards-最短路径-[解题报告] C++
2013
12-12

HDU 1535 Invitation Cards-最短路径-[解题报告] C++

Invitation Cards

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

46
210

 

大意:给你一个源点,让你从这里派发n个学生去其余的n-1个站点去邀请人们去CSS,然后再返回CSS,使得总的cost最小。

 

思路:

(1)过去的时候:求一次最短路,将所有的d[i]相加。

(2)回来的时候:我开始想把SPFA所有的点,然后相加,估计会超时。由于是有向边,可以用到一个巧妙的转移的方法,我们将有向边反向,由于题目保证所有的点均可到达,所以SPFA源点,然后相加可得结果。

CODE:

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int SIZE = 1000001;
const int INF = 0x3f3f3f3f;
int u[2*SIZE], v[2*SIZE], w[2*SIZE], next[2*SIZE];
int s_u[2*SIZE], s_v[2*SIZE], s_w[2*SIZE];
int first[SIZE], d[SIZE];
int n, m, cnt;

void init()
{
    memset(u, 0, sizeof(u));
    memset(v, 0, sizeof(v));
    memset(w, INF, sizeof(w));
    memset(d, 0, sizeof(d));
    memset(next, 0, sizeof(next));
    memset(first, -1, sizeof(first));
    cnt = 0;
}

void spfa(int src)
{
    queue<int> q;
    bool inq[SIZE] = {0};
    for(int i = 1; i <= n; i++) d[i] = (i == src)? 0:INF;
    q.push(src);
    while(!q.empty())
    {
        int x = q.front(); q.pop();
        inq[x] = 0;
        for(int e = first[x]; e!=-1; e = next[e]) if(d[v[e]] > d[x]+w[e])
        {
            d[v[e]] = d[x] + w[e];
            if(!inq[v[e]])
            {
                inq[v[e]] = 1;
                q.push(v[e]);
            }
        }
    }
}

void read_graph(int u1, int v1, int w1)
{
    u[cnt] = u1; v[cnt] = v1; w[cnt] = w1;
    next[cnt] = first[u[cnt]];
    first[u[cnt]] = cnt;
    cnt++;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int ans = 0;
        init();
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= m; i++)    scanf("%d%d%d", &s_u[i], &s_v[i], &s_w[i]);
        for(int i = 1; i <= m; i++) read_graph(s_u[i], s_v[i], s_w[i]);
        spfa(1);
        for(int i = 1; i <= n; i++) ans += d[i];
        init();
        for(int i = 1; i <= m; i++) read_graph(s_v[i], s_u[i], s_w[i]);
        spfa(1);
        for(int i = 1; i <= n; i++) ans += d[i];
        printf("%d\n", ans);
    }
    return 0;
}

解题报告转自:http://www.cnblogs.com/g0feng/archive/2012/09/14/2685520.html


  1. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。

  2. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。

  3. 5.1处,反了;“上一个操作符的优先级比操作符ch的优先级大,或栈是空的就入栈。”如代码所述,应为“上一个操作符的优先级比操作符ch的优先级小,或栈是空的就入栈。”

  4. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。