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. Codeforces Round #646 (Div. 2) 题解 (ABCDE)

    目录 A. Odd Selection B. Subsequence Hate C. Game On Leaves D. Guess The Maximums E. Tree Shuffling ht ...

  2. Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords (贪心)

    题意:你有\(a\)个树枝和\(b\)个钻石,\(2\)个树枝和\(1\)个钻石能造一个铁铲,\(1\)个树枝和\(2\)个钻石能造一把剑,问最多能造多少铲子和剑. 题解:如果\(a\le b\),若 ...

  3. HTTP笔记2--简单的HTTP 协议

    HTTP概念 HTTP用于客户端和服务器之间的通信 客户端:请求访问文本或图像等资源的一端 服务器端而提供资源响应的一端 通过请求和响应的交换达成通信 HTTP 协议规定,请求从客户端发出,最后服务器 ...

  4. nyoj-1236 挑战密室

    挑战密室 时间限制:1 s | 内存限制:128 M 提交 状态 排名 题目描述 R组织的特工Dr. Kong 为了寻找丢失的超体元素,不幸陷入WTO密室.Dr. Kong必须尽快找到解锁密码逃离,否 ...

  5. Leetcode(885)- 救生艇

    第 i 个人的体重为 people[i],每艘船可以承载的最大重量为 limit. 每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit. 返回载到每一个人所需的最小船数.(保证每个人都 ...

  6. vue & this.$router.resolve

    vue & this.$router.resolve gotoAutoUpdate (query = {}) { const { href } = this.$router.resolve({ ...

  7. Vue computed props pass params

    Vue computed props pass params vue 计算属性传参数 // 计算 spreaderAlias spreaderAlias () { console.log('this. ...

  8. Linux Schedule Cron All In One

    Linux Schedule Cron All In One 定时任务 / 定时器 GitHub Actions Scheduled events Cron syntax has five field ...

  9. 增强 CT & CT & MR

    增强 CT & CT & MR CTA,增强 CT Computed Tomography (CT) CT 计算机断层扫描 Computed Tomography (CT) Angio ...

  10. Taro 版本

    Taro 版本 https://taro-docs.jd.com/taro/versions.html 1.x 1.3.34 https://taro-docs.jd.com/taro/docs/1. ...