Invitation Cards

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2972    Accepted Submission(s): 1385

Problem 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
 
Source
 
 
题意: 一个单向图  求1到给点的最短距离 和各个点到1的最短距离之和
题解:正向反向 两次建图 前向星存图     前向星 传送门 http://blog.csdn.net/acdreamers/article/details/16902023  
struct node

{
int pr;//同起点的上一条边 
int to;//单向边的终点
int w;//边的权值
} edge1[MAXN],edge2[MAXN];// 存边

加边操作

void add1(int a,int b,int c) //a-->b
{
nedge1++;//初始为零
edge1[nedge1].to=b;
edge1[nedge1].w=c;
edge1[nedge1].pr=pre1[a];
pre1[a]=nedge1;//以a为起点的下一条边的位置(位置其实就是边的序号) pre1数组初始为0;

                            //(发现这里是逆向的 一层一层的往上找 指导 pre1数组的值为0 ) 遍历的时候
}

SPFA 姿势

 
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define MAXN 1000001
#define inf 100000000
using namespace std;
struct node
{
int pr;
int to;
int w;
}edge1[MAXN],edge2[MAXN];
int pre1[MAXN],pre2[MAXN];
int dis1[MAXN],dis2[MAXN];
int nedge1=;
int nedge2=;
int n;
int p,q;
int be,ed,we;
queue<int >qq;
int now;
int vis[MAXN];
void add1(int a,int b,int c)
{
nedge1++;
edge1[nedge1].to=b;
edge1[nedge1].w=c;
edge1[nedge1].pr=pre1[a];
pre1[a]=nedge1;
}
void add2(int a,int b,int c)
{
nedge2++;
edge2[nedge2].to=b;
edge2[nedge2].w=c;
edge2[nedge2].pr=pre2[a];
pre2[a]=nedge2;
}
void spfa1()
{
for(int gg=;gg<=p;gg++)
{
vis[gg]=;
dis1[gg]=inf;
}
vis[]=;
dis1[]=;
qq.push();
while(!qq.empty())
{
now=qq.front();
vis[now]=;
qq.pop();
for(int k=pre1[now];k!=;k=edge1[k].pr)
{
int mm=edge1[k].to;
if(dis1[now]+edge1[k].w<dis1[mm])
{
dis1[mm]=dis1[now]+edge1[k].w;
if(!vis[mm])
{
vis[mm]=;
qq.push(mm);
}
}
}
}
}
void spfa2()
{
for(int gg=;gg<=p;gg++)
{
vis[gg]=;
dis2[gg]=inf;
}
vis[]=;
dis2[]=;
qq.push();
while(!qq.empty())
{
now=qq.front();
qq.pop();
vis[now]=;
for(int k=pre2[now];k!=;k=edge2[k].pr)
{
int mm=edge2[k].to;
if(dis2[now]+edge2[k].w<dis2[mm])
{
dis2[mm]=dis2[now]+edge2[k].w;
if(!vis[mm])
{
vis[mm]=;
qq.push(mm);
}
}
}
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d %d",&p,&q);
nedge1=;nedge2=;
for(int gg=;gg<=p;gg++)
{
pre1[gg]=;
pre2[gg]=;
}
for(int j=;j<=q;j++)
{
scanf("%d %d %d",&be,&ed,&we);
add1(be,ed,we);
add2(ed,be,we);
}
spfa1();
spfa2();
int sum=;
for(int e=;e<=p;e++)
sum=sum+dis1[e]+dis2[e];
printf("%d\n",sum);
}
return ;
}
 

HDU 1535 SPFA 前向星存图优化的更多相关文章

  1. 最短路 spfa 算法 && 链式前向星存图

    推荐博客  https://i.cnblogs.com/EditPosts.aspx?opt=1 http://blog.csdn.net/mcdonnell_douglas/article/deta ...

  2. Pants On Fire(链式前向星存图、dfs)

    Pants On Fire 传送门:链接  来源:upc9653 题目描述 Donald and Mike are the leaders of the free world and haven't ...

  3. C++算法 链式前向星存图

    这个东西恶心了我一阵子,那个什么是什么的上一个一直是背下来的,上次比赛忘了,回来有个题也要用,只能再学一遍,之前也是,不会为什么不学呢.我觉得是因为他们讲的不太容易理解,所以我自己给那些不会的人们讲一 ...

  4. UESTC 30.最短路-最短路(Floyd or Spfa(链式前向星存图))

    最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...

  5. POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7536   Accepted: 3559 Case ...

  6. 链式前向星存树图和遍历它的两种方法【dfs、bfs】

    目录 一.链式前向星存图 二.两种遍历方法 一.链式前向星存图:(n个点,n-1条边) 链式前向星把上面的树图存下来,输入: 9 ///代表要存进去n个点 1 2 ///下面是n-1条边,每条边连接两 ...

  7. UESTC30-最短路-Floyd最短路、spfa+链式前向星建图

    最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...

  8. POJ——3159Candies(差分约束SPFA+前向星+各种优化)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 28071   Accepted: 7751 Descrip ...

  9. POJ 1655 Balancing Act ( 树的重心板子题,链式前向星建图)

    题意: 给你一个由n个节点n-1条边构成的一棵树,你需要输出树的重心是那个节点,以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的 题解: 树的重心定义:找到一个点,其所 ...

随机推荐

  1. lintcode373 奇偶分割数组

    奇偶分割数组 分割一个整数数组,使得奇数在前偶数在后. 您在真实的面试中是否遇到过这个题? Yes 样例 给定 [1, 2, 3, 4],返回 [1, 3, 2, 4]. 我的方法:设定两个数组,分别 ...

  2. 菜鸟之路——机器学习之决策树个人理解及Python实现

    最近开始学习机器学习,以下会记录我学习中遇到的问题以及我个人的理解 决策树算法,网上很多介绍,在这不复制粘贴.下面解释几个关键词就好. 信息熵(entropy):就是信息不确定性的多少 H(x)=-Σ ...

  3. UVa 10082 - WERTYU 解题报告 - C语言

    1.题目大意: 输入一个错位的字符串(字母全为大写),输出原本想打出的句子. 2.思路: 如果将每个输入字符所对应的应输出字符一一使用if或者switch,则过于繁琐.因此考虑使用常量数组实现. 3. ...

  4. HDU 2485 Destroying the bus stations(!最大流∩!费用流∩搜索)

    Description Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “im ...

  5. 软件工程-东北师大站-第二次作业psp

    1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 本周PSP饼状图

  6. 百度编辑器ueditor的图片地址修正

    我用的百度编辑器为1.4.2的,相对于现在这个时间来说是比较新的.之前去的1.3版的,后来更新到1.4之后出现路径问题.因为今天晚上出现特别奇怪的问题,所以特地又整了一遍,发现这玩意还是得自己弄通了好 ...

  7. VS2013运行C++报错:This function or variable may be unsafe. Consider using fopen_s instead.

    在vs2013中运行时发生的关于方法调用的不安全错误. 1.更换方法,但是有些方法更改后参数不变,所以可能比较麻烦. 2.添加一条预处理器定义: 点击项目——>属性——>c/c++——&g ...

  8. mysql唯一查询

    MySQL单一字段唯一其他字段差异性忽略查询.在使用MySQL时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返 ...

  9. LintCode-165.合并两个排序链表

    合并两个排序链表 将两个排序链表合并为一个新的排序链表 样例 给出 1->3->8->11->15->null,2->null, 返回 1->2->3- ...

  10. 3ds Max学习日记(一)

      暑假闲来无事学习一发3ds Max.为啥要学这玩意?貌似可以用这东西三维建模.暑期生产实习选了一个搞vr的导师,貌似他忙得很,无奈只好先自己研究一下啦~   vr神马的还是有点意思的,虽然自己仅仅 ...