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. Java应用基础微专业-进阶篇

    第1章--使用对象 1.1 字符类型 char c = 65; // char --> int char c = '\u0041'; // \u: unicode + (Hex 41--> ...

  2. 关于html2canvas清晰度

    最近有个小项目 需要生成海报让用户去分享~~~vue做的,海报通过html2canvas 生成. 遇到的最大问题是生成图片的清晰度~~网上找了好多方法. 放大倍数!~网上找的~~ var cntEle ...

  3. Django学习总结①

    Django基础环境配置好以后,打开pycharm,创建Django项目 视图views 中需要导入 django.http ---> HttpResponse models库 - 常用方法: ...

  4. 服务器返回中文乱码的情况(UTF8编码 -> 转化为 SYSTEM_LOCALE 编码)

    服务器乱码 转换使用如下方法 入惨{“msg”} -> utf8编码 -> 转化为 SYSTEM_LOCALE 编码 -> 接受转换后的参数 "sEncoding" ...

  5. 5.安装hbase

    下载安装包并解压设置hbase环境变量配置hbase-site.xml启动hbase检测hbase启动情况测试hbase shell 下载安装包并解压 https://mirrors.tuna.tsi ...

  6. HADOOP docker(一):安装hadoop实验集群(略操蛋)

    一.环境准备 1.1.机器规划 主机名    别名    IP     角色 9321a27a2b91 hadoop1 172.17.0.10 NN1 ZK RM 7c3a3c9cd595 hadoo ...

  7. “Hello World!”团队第三周召开的第一次会议

    今天是我们团队“Hello World!”团队第三周召开的第一次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.Todo List 六.会议照片 七.燃尽图 一.会议时间 ...

  8. 20172330 2017-2018-1 《Java程序设计》第七周学习总结

    学号 2017-2018-1 <程序设计与数据结构>第七周学习总结 教材学习内容总结 这一章主要是对继承的学习: 继承是组织和创建类的基本技术,概念简单但影响重大,决定着面向对象软件的设计 ...

  9. 计算器软件实现系列(五)策略模式+asp.net

    一 策略模式代码的编写 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// ...

  10. [基于NetCore的简单博客系统]-登录

    0-项目背景 一个基于.NET CORE RAZOR PAGES的简单博客系统 技术栈全部采用微软官方实现方式,目的是熟悉新技术 项目地址:https://github.com/ganqiyin/BL ...