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. 迪杰斯特拉+拆点 Deliver the Cake - HDU 6805

    题意: t组输入,给你n个点m条边.你需要输出从s点到t点的最短距离,然后是m条边,每条边输入信息为: a,b,c 表示从a点到b点的一个无向边长度为c 每一个点会有一个属性L.R或M 如果a和b一个 ...

  3. Codeforces Round #547 (Div. 3) E. Superhero Battle (数学)

    题意:有一个HP为\(h\)的大怪兽,你需要轮流进行\(i\)次操作.每次可以使\(h+=d_i\)(\(d_i\)有正有负),当第\(n\)次操作完成后,再从第一次开始,问能否使得怪兽的HP变为\( ...

  4. Codeforces #637 div2 B. Nastya and Door

    题意:给你一个数组a,定义:若a[i]>a[i]&&a[i]>a[i-1],则a[i]为峰值,求长度为k的区间内峰值最多能为多少,并输出这个区间的左端点(区间需要将峰的左边 ...

  5. CF1462-D. Add to Neighbour and Remove

    codeforces1462D 题意: 给出一个由n个数组成的数组,现在你可以对这个数组进行如下操作:将数组中的一个元素加到这个元素的两边中的一边,然后将这个元素删掉.若该元素在最左边,那么该元素不能 ...

  6. 牛客网多校第3场 C-shuffle card 【splay伸展树】

    题目链接:戳这里 转自:戳这里 关于splay入门:戳这里 题意:给n个数,进行m次操作,每次都从n个数中取出连续的数放在最前面. 解题思路:splay的区间操作. 附代码: 1 #include&l ...

  7. ZOJ 3494 BCD Code(AC自动机 + 数位DP)题解

    题意:每位十进制数都能转化为4位二进制数,比如9是1001,127是 000100100111,现在问你,在L到R(R <= $10^{200}$)范围内,有多少数字的二进制表达式不包含模式串. ...

  8. 翻译:《实用的Python编程》01_04_Strings

    目录 | 上一节 (1.3 数字) | 下一节 (1.5 列表) 1.4 字符串 本节介绍处理文本的方法. 表示字面量文本 在程序中字符串字面量使用引号来书写. # 单引号(Single quote) ...

  9. pagehide event & sendBeacon

    pagehide event & sendBeacon 通过 API 测试 pagehide 是否触发了 pagehide 不支持正常的 fetch 请求发送 pagehide 仅支持 sen ...

  10. Swift All in One

    Swift All in One Swift 5.3 https://github.com/apple/swift-evolution Xcode https://developer.apple.co ...