POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))
题目链接:http://poj.org/problem?id=1511
题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9。问你从起点到各个点以及从各个点到起点的最小路程总和。
解题思路:这里用了优先队列优化的dijkstra复杂度mlogn,从起点到个点最短路径直接算就好了,算各个点到起点的最短路径,只要把边的方向反一下,再算一次从起点到个点最短路径就好了。
Dijkstra:
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<functional>
- #include<vector>
- #include<queue>
- using namespace std;
- typedef long long LL;
- typedef pair<int,int> p;
- const int N=1e6+;
- const int INF=<<;//这里不能比1e9小
- struct edge{
- int to,cost;
- };
- vector<edge>eg[N];
- int V,E;
- bool used[N];
- int d[N];
- int a[N],b[N],c[N];
- void dijkstra(int s){
- for(int i=;i<=V;i++){
- d[i]=INF;
- used[i]=false;
- }
- d[s]=;
- priority_queue<p,vector<p>,greater<p> >q;
- q.push(p(,s));
- while(!q.empty()){
- p p1=q.top();
- q.pop();
- int v=p1.second;
- if(used[v]) continue;
- used[v]=true;
- for(int i=;i<eg[v].size();i++){
- edge e=eg[v][i];
- if(d[e.to]>d[v]+e.cost){
- d[e.to]=d[v]+e.cost;
- q.push(p(d[e.to],e.to));
- }
- }
- }
- }
- int main(){
- int q;
- scanf("%d",&q);
- while(q--){
- scanf("%d %d",&V,&E);
- for(int i=;i<=V;i++){
- eg[i].clear();
- }
- for(int i=;i<=E;i++){
- scanf("%d%d%d",&a[i],&b[i],&c[i]);
- edge gg;
- gg.to=b[i],gg.cost=c[i];
- eg[a[i]].push_back(gg);
- }
- dijkstra();
- LL sum=;
- for(int i=;i<=V;i++){
- sum+=d[i];
- }
- //将边的方向反一下
- for(int i=;i<=V;i++){
- eg[i].clear();
- }
- for(int i=;i<=E;i++){
- edge gg;
- gg.to=a[i],gg.cost=c[i];
- eg[b[i]].push_back(gg);
- }
- dijkstra();
- for(int i=;i<=V;i++){
- sum+=d[i];
- }
- printf("%lld\n",sum);
- }
- }
SPFA:
- #include<iostream>
- #include<queue>
- #include<vector>
- using namespace std;
- const int INF=<<;
- const int N=1e6+;
- struct edge{
- int to,cost;
- };
- vector<edge>eg[N];
- int V,E;
- bool used[N];//是否在队列中
- int d[N];
- int a[N],b[N],c[N];
- void spfa(int s){
- for(int i=;i<=V;i++){
- d[i]=INF;
- used[i]=false;
- }
- d[s]=;
- queue<int>q;
- q.push(s);
- used[s]=true;
- while(!q.empty()){
- int v=q.front();
- q.pop();
- used[v]=false;
- for(int i=;i<eg[v].size();i++){
- edge e=eg[v][i];
- if(d[e.to]>d[v]+e.cost){
- d[e.to]=d[v]+e.cost;
- if(!used[e.to]){
- used[e.to]=true;
- q.push(e.to);
- }
- }
- }
- }
- }
- int main(){
- int q;
- scanf("%d",&q);
- while(q--){
- scanf("%d%d",&V,&E);
- for(int i=;i<=V;i++){
- eg[i].clear();
- }
- for(int i=;i<=E;i++){
- scanf("%d%d%d",&a[i],&b[i],&c[i]);
- edge gg;
- gg.to=b[i],gg.cost=c[i];
- eg[a[i]].push_back(gg);
- }
- spfa();
- long long sum=;
- for(int i=;i<=V;i++){
- sum+=d[i];
- }
- //方向反一下
- for(int i=;i<=V;i++){
- eg[i].clear();
- }
- for(int i=;i<=E;i++){
- edge gg;
- gg.to=a[i],gg.cost=c[i];
- eg[b[i]].push_back(gg);
- }
- spfa();
- for(int i=;i<=V;i++){
- sum+=d[i];
- }
- printf("%lld\n",sum);
- }
- }
POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))的更多相关文章
- POJ 1511 - Invitation Cards (dijkstra优先队列)
题目链接:http://poj.org/problem?id=1511 就是求从起点到其他点的最短距离加上其他点到起点的最短距离的和 , 注意路是单向的. 因为点和边很多, 所以用dijkstra优先 ...
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 16178 Accepted: 526 ...
- POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- POJ 1511 - Invitation Cards 邻接表 Dijkstra堆优化
昨天的题太水了,堆优化跑的不爽,今天换了一个题,1000000个点,1000000条边= = 试一试邻接表 写的过程中遇到了一些问题,由于习惯于把数据结构封装在 struct 里,结果 int [10 ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- SPFA算法(2) POJ 1511 Invitation Cards
原题: Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 31230 Accepted: ...
- poj 1511 Invitation Cards spfa 邻接矩阵
题目链接: http://poj.org/problem?id=1511 题目大意: 这道题目比较难理解,我读了好长时间,最后还是在队友的帮助下理解了题意,大意就是,以一为起点,求从一到其他各点的最短 ...
随机推荐
- Kerberos的白银票据详解
0x01白银票据(Silver Tickets)定义 白银票据(Silver Tickets)是伪造Kerberos票证授予服务(TGS)的票也称为服务票据.如下图所示,与域控制器没有AS-REQ 和 ...
- python基础----__slots__方法、__call__方法
''' 1.__slots__是什么:是一个类变量,变量值可以是列表,元祖,或者可迭代对象,也可以是一个字符串(意味着所有实例只有一个数据属性) 2.引子:使用点来访问属性本质就是在访问类或者对象的_ ...
- pycrypto 安装
https://www.dlitz.net/software/pycrypto/ 下载pycrypto-2.6.1.tar.gz,解压后 python setup.py build python se ...
- MySQL自定义函数、触发器、存储过程
存储过程 概念 存储过程,是一个数据库对象,类似一个函数. 在存储过程中可以使用SQL中的绝大部分内容,并且可以加入编程语言的特性(循环判断分支). 编写好存储过程之后,可以在客户端调用存储过程,存储 ...
- springMVC的controller返回值
1.可以返回ModelAndView 2.可以返回一个String字符串:即一个jsp页面的逻辑视图名,这个在springMVC.xml中可以配置此页面逻辑视图的前缀和后缀 3.可以返回void类型: ...
- STL源码分析-bitset
http://note.youdao.com/noteshare?id=0ea12c1fffd3866c6eddb8dc208c109d
- 【题解】Points, Lines and Ready-made Titles Codeforces 871C 图论
Prelude 真是一道好题,然而比赛的时候花了太多时间在B题上,没时间想这个了QAQ. 题目链接:萌萌哒传送门(.^▽^) Solution 观察样例和样例解释,我们发现,假如有四个点,恰好占据在某 ...
- PL/SQL Developer 中的问题:Initialization error Could not load ".../oci.dll"解决方法
---------------------------------------------------------------------------------------------------- ...
- bootstrap模态框 内部input无法手动获取焦点
//重写enforceFocus方法$(document).ready(function(){ $.fn.modal.Constructor.prototype.enforceFocus = func ...
- GridControl详解(三)列数据的格式设置
为了测试方便,我们加入新的3列,格式分别是数据,时间,字符串.代码增加下列部分: //格式增加 dt.Columns.Add("数据",typeof(decimal)); dt.C ...