Candies

POJ-3159

这里是图论的一个应用,也就是差分约束。通过差分约束变换出一个图,再使用Dijikstra算法的链表优化形式而不是vector形式(否则超时)。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
int n,m;
struct edge{
int to;
int cost;
int next;
};
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;
}
};
edge ma[150005];
int head[30004];
int top;//指向头结点
int d[30004];
void addedge(int a,int b,int c){
ma[top].to=b;
ma[top].cost=c;
ma[top].next=head[a];
head[a]=top;
top++;
}
void dijikstra(int s){
priority_queue<node> que;
for(int i=1;i<=n;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;
for(int h=head[v];h!=-1;h=ma[h].next){
edge e=ma[h];
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(){
memset(head,-1,sizeof(head));
top=0;
scanf("%d%d",&n,&m);
int a,b,c;
for(int i=0;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
}
dijikstra(1);
cout<<d[n]<<endl;
//system("pause");
return 0;
}

java:PriorityQueue+Vector

package POJ;
import java.io.*;
import java.util.*;
import java.util.Vector;
public class POJ_3159 {
static final int INF=0x3f3f3f3f;
static int n,m;
static class edge{
int to;
int cost;
edge(){}
edge(int a,int b){to=a;cost=b;}
};
static class node implements Comparable<node>{
int dis;
int to;
node(){}
node(int a,int b){dis=a;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 Vector<edge> []G;//[30004];
static int []d;//[30004];
static void dijkstra(int s){
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;
for(int i=0;i<G[v].size();i++){
edge e=G[v].elementAt(i);
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) throws IOException {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
n=(int)in.nval;//cin.nextInt();
in.nextToken();
m=(int)in.nval;//cin.nextInt();
G=new Vector[n+1];
d=new int[n+1];
for(int i=0;i<=n;i++)
G[i]=new Vector<edge>();
for(int i=0;i<m;i++) {
in.nextToken();
int a=(int)in.nval;//cin.nextInt();
in.nextToken();
int b=(int)in.nval;//cin.nextInt();
in.nextToken();
int c=(int)in.nval;//cin.nextInt();
G[a].add(new edge(b,c));
}
dijkstra(1);
System.out.println(d[n]);
} }

java:PriorityQueue+向前星

package POJ;
import java.io.*;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;
public class POJ_3159_2 {
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 int []head; static int top; 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){
es[top]=new edge(b,c,head[a]);
head[a]=top;
top++; } static void dijkstra(int s) {
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;
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));
}
}
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
top=0;
in.nextToken();
n=(int)in.nval;//cin.nextInt();
in.nextToken();
m=(int)in.nval;//cin.nextInt();
es=new edge[m];
head=new int[n+1];
Arrays.fill(head, -1);
d=new int[n+1];
for(int i=0;i<m;i++) {
int from,to,price;
in.nextToken();
from=(int)in.nval;//cin.nextInt();
in.nextToken();
to=(int)in.nval;//cin.nextInt();
in.nextToken();
price=(int)in.nval;//cin.nextInt();
addedge(from,to,price);
} dijkstra(1);
System.out.println(d[n]); } }

POJ-3159(差分约束+Dijikstra算法+Vector优化+向前星优化+java快速输入输出)的更多相关文章

  1. poj 3159(差分约束经典题)

    题目链接:http://poj.org/problem?id=3159思路:题目意思很简单,都与给定的条件dist[b]-dist[a]<=c,求dist[n]-dist[1]的最大值,显然这是 ...

  2. poj 3159 差分约束

    思路:班长的糖果要比snoopy的多.并且要用手写堆栈,且堆栈的大小要开到20000000. #include<iostream> #include<cstdio> #incl ...

  3. Candies POJ - 3159 差分约束

    // #include<iostream> #include<cstring> #include<queue> #include<stack> #inc ...

  4. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

  5. poj Layout 差分约束+SPFA

    题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...

  6. poj 1201 差分约束

    http://www.cnblogs.com/wangfang20/p/3196858.html 题意: 求集合Z中至少要包含多少个元素才能是每个区间[ai,bi]中的元素与Z中的元素重合个数为ci. ...

  7. POJ - 3169 差分约束

    题意:n头牛,按照编号从左到右排列,两头牛可能在一起,接着有一些关系表示第a头牛与第b头牛相隔最多与最少的距离,最后求出第一头牛与最后一头牛的最大距离是多少,如         果最大距离无限大则输出 ...

  8. POJ 1201 差分约束+SPFA

    思路: 差分约束,难在建图.(我是不会告诉你我刚学会SPFA的...) 把每个区间的ai–>bi连一条长度为ci的边. k–>k+1连一条长度为0的边. k+1–>k连一条长度为-1 ...

  9. POJ 1201 差分约束(集合最小元素个数)

    题意:       给你一个集合,然后有如下输入,a ,b ,c表示在范围[a,b]里面有至少有c个元素,最后问你整个集合最少多少个元素. 思路:       和HDU1384一模一样,首先这个题目可 ...

随机推荐

  1. 【uva 11054】Wine trading in Gergovia(算法效率--等价转换)

    题意:N个等距村庄,买(>0)卖(<0)酒,供需平衡,运K则需K劳动力.问所需的最小劳动力. 解法:由于运出或运入1的都需经过2,所以无论如何,都可以等价于从2本身运入或运出.因此可以将1 ...

  2. hdu2126 Buy the souvenirs

    Problem Description When the winter holiday comes, a lot of people will have a trip. Generally, ther ...

  3. CQRS Event Sourcing介绍

    什么是CQRS模式? CQRS是Command and Query Responsibility Segregation的缩写,直译就是命令与查询责任分离的意思. 命令会改变对象的状态,但不返回任何数 ...

  4. 同时拿到BATJMD的Offer是怎样的一种体验?

    写在前面 又到了收割Offer的季节,你准备好了吗?曾经的我,横扫各个大厂的Offer.还是那句话:进大厂临时抱佛脚是肯定不行的,一定要注重平时的总结和积累,多思考,多积累,多总结,多复盘,将工作经历 ...

  5. Kubernets二进制安装(4)之Docker安装

    注意:需要安装Docker的机器为mfyxw30.mfyxw40.mfyxw50 集群规划 主机名 角色 IP地址 mfyxw30.mfyxw.com Docker 192.168.80.30 mfy ...

  6. HTTP常见状态码(200、301、302、404、500、502)详解

         概述 运维工作中,在应用部署的时候,通常遇到各种HTTP的状态码,我们比较常见的如:200.301.302.404.500.502 等,有必要整理一份常见状态码的文档,加深印象,方便回顾. ...

  7. 康托展开:对全排列的HASH和还原,判断搜索中的某个排列是否出现过

    题目:http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2297 前置技能:(千万注意是 ...

  8. MS16-032 windows本地提权

    试用系统:Tested on x32 Win7, x64 Win8, x64 2k12R2 提权powershell脚本: https://github.com/FuzzySecurity/Power ...

  9. SQL All In One

    SQL All In One Structured Query Language SQL is an ANSI (American National Standards Institute) stan ...

  10. Travis CI in Action

    Travis CI in Action node.js https://docs.travis-ci.com/user/tutorial/ https://docs.travis-ci.com/use ...