POJ-1511(Dijkstra+优先队列优化+向前星)
Invitation Cards
POJ-1511
- 从这道题我还是发现了很多的问题,首先就是快速输入输出,这里的ios::---这一行必须先放在main函数第一行,也就是输入最开始的前面,否则系统疯狂报WA。
- 其次就是,ios的位置没有错之后又疯狂地报TLE,就是超时了,这个问题要不就是算法的复杂度,还有就是输入输出还是不够快,所以首先排除输入输出的问题,所以我把ios改成了scanf所以这题就过了。
- 事实证明,ios的方法还是没有scanf快,所以以后还是使用scanf.
- 其次就是这个算法本身的问题,这个其实已经比n*n的算法快多了,由于本题的数据量太大,这个版本也是很险才过的。
- 关于本题的思路主要就是正着走一遍,然后倒着走一遍,最后累加起来就可以了。
- 推荐一个多方法的博客:https://blog.csdn.net/qq_39665840/article/details/81437812
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
int p,q;//p-stops;q-lines
struct edge{
int to;
int cost;
edge(){}
edge(int a,int b):to(a),cost(b){}
};
struct node{
int dis;
int to;
node(){}
node(int a,int b):dis(a),to(b){}
bool operator<(const node& t)const{
return dis>t.dis;
}
};
vector<edge> G[1000006];
vector<edge> rG[1000006];
long long d[1000006];
void dijikstra(int s,int type){
priority_queue<node> que;
for(int i=1;i<=p;i++){
d[i]=INF;
}
d[s]=0;
que.push(node(0,s));
while(!que.empty()){
node temp=que.top();
que.pop();
int v=temp.to;
if(d[v]<temp.dis)
continue;
if(type==1){
for(int i=0;i<G[v].size();i++){
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
que.push(node(d[e.to],e.to));
}
}
}else{
for(int i=0;i<rG[v].size();i++){
edge e=rG[v][i];
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
que.push(node(d[e.to],e.to));
}
}
}
}
}
int main(){
// ios::sync_with_stdio(false);
// cin.tie(0);
int t;
cin>>t;
while(t--){
scanf("%d%d",&p,&q);
int s,e,w;
memset(G,0,sizeof(G));
memset(rG,0,sizeof(rG));
for(int i=0;i<q;i++){
scanf("%d%d%d",&s,&e,&w);
G[s].push_back(edge(e,w));
rG[e].push_back(edge(s,w));
}
dijikstra(1,1);
long long sum=0;
for(int i=1;i<=p;i++){
sum+=d[i];
}
dijikstra(1,2);
for(int i=1;i<=p;i++){
sum+=d[i];
}
cout<<sum<<endl;
}
return 0;
}
java
package POJ;
import java.util.*;
import java.util.PriorityQueue;
public class POJ_1511 {
static int t,n,m;//1 <= n,m <= 1000000
static final int INF=0X3F3F3F3F;
static class edge{
public int to,cost,next;
edge(){}
edge(int to,int cost,int next){
this.to=to;
this.cost=cost;
this.next=next;
}
};
static edge []es;
static edge []es1;
static int []head;
static int []head1;
static int top;
static int top1;
static int []d;
static class node implements Comparable<node>{
public int dis,to;
node(){}
node(int a,int b){
this.dis=a;
this.to=b;
}
@Override
public int compareTo(node b) {
// TODO Auto-generated method stub
if(dis>b.dis)
return -1;
else if(dis==b.dis)
return 0;
else return 1;
}
};
static void addedge(int a,int b,int c,int type){
if(type==1) {
es[top]=new edge(b,c,head[a]);
head[a]=top;
top++;
}else {
es1[top1]=new edge(b,c,head1[a]);
head1[a]=top1;
top1++;
}
}
static void dijkstra(int s,int type) {
PriorityQueue<node>que=new PriorityQueue<node>();
for(int i=1;i<=n;i++){
d[i]=INF;
}
d[s]=0;
que.add(new node(0,s));
while(!que.isEmpty()){
node temp=que.poll();
int v=temp.to;
if(d[v]<temp.dis)
continue;
if(type==1) {
for(int h=head[v];h!=-1;h=es[h].next){
edge e=es[h];
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
que.add(new node(d[e.to],e.to));
}
}
}else {
for(int h=head1[v];h!=-1;h=es1[h].next){
edge e=es1[h];
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
que.add(new node(d[e.to],e.to));
}
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
t=cin.nextInt();
while(t!=0) {
top=0;top1=0;
n=cin.nextInt();
m=cin.nextInt();
es=new edge[m];
es1=new edge[m];
head=new int[n+1];
Arrays.fill(head, -1);
head1=new int[n+1];
Arrays.fill(head1, -1);
d=new int[n+1];
for(int i=0;i<m;i++) {
int from,to,price;
from=cin.nextInt();
to=cin.nextInt();
price=cin.nextInt();
addedge(from,to,price,1);
addedge(to,from,price,2);
}
dijkstra(1,1);
long sum=0;
for(int i=1;i<=n;i++){
sum+=d[i];
}
dijkstra(1,2);
for(int i=1;i<=n;i++){
sum+=d[i];
}
System.out.println(sum);
t--;
}
}
}
POJ-1511(Dijkstra+优先队列优化+向前星)的更多相关文章
- 地铁 Dijkstra(优先队列优化) 湖南省第12届省赛
传送门:地铁 思路:拆点,最短路:拆点比较复杂,所以对边进行最短路,spfa会tle,所以改用Dijkstra(优先队列优化) 模板 /******************************** ...
- 【bzo1579】拆点+dijkstra优先队列优化+其他优化
题意: n个点,m条边,问从1走到n的最短路,其中有K次机会可以让一条路的权值变成0.1≤N≤10000;1≤M≤500000;1≤K≤20 题解: 拆点,一个点拆成K个,分别表示到了这个点时还有多少 ...
- 晴天小猪历险记之Hill(Dijkstra优先队列优化)
描述 这一天,他来到了一座深山的山脚下,因为只有这座深山中的一位隐者才知道这种药草的所在.但是上山的路错综复杂,由于小小猪的病情,晴天小猪想找一条需时最少的路到达山顶,但现在它一头雾水,所以向你求助. ...
- POJ-3159(差分约束+Dijikstra算法+Vector优化+向前星优化+java快速输入输出)
Candies POJ-3159 这里是图论的一个应用,也就是差分约束.通过差分约束变换出一个图,再使用Dijikstra算法的链表优化形式而不是vector形式(否则超时). #include< ...
- 最短路--dijkstra+优先队列优化模板
不写普通模板了,还是需要优先队列优化的昂 #include<stdio.h> //基本需要的头文件 #include<string.h> #include<queue&g ...
- (模板)poj2387(dijkstra+优先队列优化模板题)
题目链接:https://vjudge.net/problem/POJ-2387 题意:给n个点(<=1000),m条边(<=2000),求结点n到结点1的最短路. 思路:dijkstra ...
- Dijkstra + 优先队列优化 模板
#include <cstdio> #include <cstring> #include <queue> #include <vector> #inc ...
- Dijkstra优先队列优化
Dijkstra算法的核心思想就是两步排序,一个是对于一个点而言,他的最小边要经过所有其他点最小边的测试才能确认,也就是说要在这其中找一个最大的边出来:第二个是对于每次循环而言的,每次的更新d数组都是 ...
- Dijkstra 优先队列优化
#include <iostream> #include <queue> #include <vector> using namespace std; ; stru ...
随机推荐
- 【51nod1674】区间的价值 V2(算法效率--位运算合并优化+链表实现)
题目链接: 51nod1674 题意:规定一个区间的价值为这个区间中所有数and起来的值与这个区间所有数or起来的值的乘积.现在l有一个 N 个数的序列,问所有n*(n+1)/2个区间的贡献的和对1 ...
- Eclipse无法打开提示could not open jvm.cfg错误
先言:我的问题只是出在之前我的java_jdk原来安装在F:\Temp这里,但是我之后在整理文件的时候把这个文件夹改成了F:\java_jdk.所以导致了错误的出现 解决方法1: 因为我的只是目录名称 ...
- C# ArrayList和List的区别
ArrayList存的是object对象,可以装任何类型,但涉及装箱拆箱,效率低,类型转换可能报错 List只能存一种类型,不涉及装箱拆箱,效率高 总结:一般情况用List吧
- XV6学习(15)Lab mmap: Mmap
代码在Github上. 这一个实验是要实现最基础的mmap功能.mmap即内存映射文件,将一个文件直接映射到内存当中,之后对文件的读写就可以直接通过对内存进行读写来进行,而对文件的同步则由操作系统来负 ...
- keras BatchNormalization 之坑
任务简述:最近做一个图像分类的任务, 一开始拿vgg跑一个baseline,输出看起来很正常: 随后,我尝试其他的一些经典的模型架构,比如resnet50, xception,但训练输出显示明显异常: ...
- Nginx location相关配置说明
Nginx location相关配置说明 基于不同的IP.不同的端口以及不用得域名实现不同的虚拟主机,依赖于核心模块ngx_http_core_module实现. 新建PC web站点 [ ...
- μC/OS-III---I笔记1---概述
在裸板上跑一些程序对于一些电子设计是足够的,所谓裸板上的程序就是传统的前后台系统,而我的理解它应该是一种"过程类"的程序,一个大循环(作为后台)做一些处理,中断程序(作为前台)来处 ...
- React Portal All In One
React Portal All In One react multi root https://reactjs.org/docs/portals.html https://zh-hans.react ...
- JavaScript var, let, const difference All In One
JavaScript var, let, const difference All In One js var, let, const 区别 All In One 是否存在 hoisting var ...
- Docker In Action
Docker In Action Docker 实战 https://docs.docker.com/get-started/overview/ Docker Engine Docker Archit ...