【POJ - 1511】 Invitation Cards(Dijkstra + 反向建图 多源到单源最短路的处理)

题干:

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

解题报告:

   就是题目比较难懂啊。。。大概意思是一群人白天从CCS(一号顶点)出发前往各个点,然后晚上再从各个点回到CCS,问你最小花费是多少。不知道题干扯了这么多没用的是干嘛。。。直接正反跑两遍DIjkstra就可以。

AC代码:

#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int MAX = 1000000 + 5;
int n,m;
int cnt;
int a[MAX],b[MAX];
int head[MAX];
bool vis[MAX];
ll c[MAX],dis1[MAX],dis2[MAX];
struct Edge {
    int to;
    ll w;
    int ne;
} e[MAX];
struct Point {
    int pos;
    ll c;
    Point(){}
    Point(int pos,int c):pos(pos),c(c){};
    bool operator < (const Point b) const {
        return c>b.c;
    }
};
void Dijkstra(ll dis[]) {
    memset(vis,0,sizeof(vis));
    for(int i = 1; i<=n; i++) dis[i] = 9999999999;
    dis[1] = 0;
    priority_queue<Point> pq;
    Point cur = Point(1,0);
    pq.push(cur);
    while(!pq.empty()) {
        cur = pq.top();
        pq.pop();
        vis[cur.c] = 1;
        for(int i = head[cur.pos]; i!=-1; i=e[i].ne) {
            if(/*!vis[e[i].to] &&*/ dis[e[i].to] > dis[cur.pos] + e[i].w) {
                dis[e[i].to ] = dis[cur.pos ] + e[i].w;
                pq.push(Point(e[i].to,dis[e[i].to ]) );
            }
        }
    }

}
void init() {
    memset(vis,0,sizeof(vis));
    memset(head ,-1,sizeof(head));
    cnt = 0;
}
void add(int u,int v,int w) {
    e[cnt].to = v;
    e[cnt].w = w;
    e[cnt].ne = head[u];
    head[u] = cnt++;
}
int main()
{
    int t;
    cin>>t;
    while(t--) {
        init();
        scanf("%d%d",&n,&m);
        for(int i = 1; i<=m; i++) {
            scanf("%d%d%lld",&a[i],&b[i],&c[i]);
            add(a[i],b[i],c[i]);
        }
 //       cout<<"jintu1111"<<endl;
        Dijkstra(dis1);
 //       cout<<"jintu1111"<<endl;
        //第两遍初始化
        init();
        for(int i = 1; i<=m; i++) {
            add(b[i],a[i],c[i] );
        }
  //      cout<<"jintu2222"<<endl;
        Dijkstra(dis2);
  //      cout<<"jintu2222"<<endl;
        ll sum = 0;
        for(int i = 1; i<=n; i++) {
            sum +=dis1[i]+dis2[i];
        }
        printf("%lld\n",sum);
    }

    return 0 ;
}

总结:

    head别初始化成0了。。。dis别忘初始化,,这里是longlong类型所以初始化成9999999999就行。。。显然这题要用longlong啊看看数据范围就知道了。

全部评论

相关推荐

04-03 22:41
兰州大学 C++
老六f:有时候是HR发错了,我之前投的百度的后端开发,他给我发的算法工程师,但是确实面的就是百度开发
点赞 评论 收藏
分享
04-16 10:27
已编辑
美团_Saas_后端开发
今天周一休息,突发奇想写一篇阶段总结。如题,我已经去了一个和Java彻底毫无关联的行业。曾经我以为自己能在计算机行业发光发热,拿到美团offer那会感觉自己天都亮了。没想到刚入行一年多就当了逃兵。从最开始的热爱到现在一看到代码就厌恶,不知道自己经历了什么。所以我去干什么了?答案是:在成都当了租房销售。上班那会压力大了就念叨着去干租房中介,但是一直下不去这个决心,想着自己学了四年多的计算机知识,终究还是不甘心。终于在某一天准备八股文的时候,看着无数篇和工作内容关系不大的理论知识,那一刻下定决心,决定尝试一下销售行业,也算是给自己一个交代。后面阴差阳错的投了成都自如去当租房管家,没想到面试很顺利,在当天一百多个面试的人里面,我成为了为数不多通过的几个幸运儿之一。目前已经培训通过,正式入职,也开了单,有压力但是每天过得很开心,真心喜欢那种和人交流的感觉,哪怕是最后没有选择找我租房。说这些也是想告诉那些大三,大四正在找Java实习而焦虑的同学:你们现在还年轻,选择很多,容错率也很高,可以尽情去尝试自己喜欢的行业和工作。不用因为某一次的面试没通过或者简历石沉大海而焦虑,更不用因为身边人都在挤编程的独木桥就强迫自己跟风。也算是自己的碎碎念吧,也希望自己能在新的领域取得一点小成就。也祝牛油工作顺利!
沉淀小子:干啥都不丢人啊,生存是必须要的,销售很考验一个人综合素质能力的,好的销售人脉和资源可不比写字楼的白领差啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务