POJ-3159-Candies(差分约束系统,入门)

题目链接:http://poj.org/problem?id=3159

题目大意:一些小朋友分糖,小朋友们的糖果数量的差别在一定范围内是可以被小朋友接受的。

输入格式:a b c,说明,满足b-a<=c就可以被小朋友a接受。输出n最多能比1多多少个糖;

思路:差分约束系统的入门题,一个板子,spfa就行,之后看了一下讨论区,说dijkstra也行,打了一遍之后发现TLE,最后加了快读勉强过,看别人的好像不加快读也轻松AC,比较了一下,发现了dijkstra的一个小的优化点(1499ms->266ms)。

AC:DFS_SPFA

//#pragma comment(linker, "/STACK:1024000000,1024000000") 
 
#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<fstream>
#include<iostream>  
#include<algorithm>  
using namespace std;  
 
#define ll long long  
#define Pair pair<int,int>
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// ?? 
//std::ios::sync_with_stdio(false);
const int MAXN=2e5+10;
const int INF32=0x3f3f3f3f;
const ll INF64=0x3f3f3f3f3f3f3f3f;
const ll mod=1e9+7;
const double PI=acos(-1.0); 

//需要自己写文件读入scanf和getchar()都不能用。
//freopen("1.txt","r",stdin);
namespace fastIO {
	#define BUF_SIZE 100000
	//fread -> read
	bool IOerror = 0;
	inline char nc() {
		static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
		if(p1 == pend) {
			p1 = buf;
			pend = buf + fread(buf, 1, BUF_SIZE, stdin);
			if(pend == p1) {
				IOerror = 1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
	}
	inline void read(int &x) {
		char ch;
		while(blank(ch = nc()));
		if(IOerror) return;
		for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
	}
	#undef BUF_SIZE
};
using namespace fastIO;


struct node{
	int v,nxt,val;
	node(int _v=0,int _val=0,int _nxt=0){
		v=_v;val=_val;nxt=_nxt;
	}
}edge[MAXN<<1];
int head[MAXN],ecnt;
int dis[MAXN],vis[MAXN];
int st[MAXN],tot;
int n,m;

void intt(){
	clean(head,-1);
	clean(dis,INF32);
	clean(vis,0);
	ecnt=0;tot=0;
}
void add(int u,int v,int val){
	edge[ecnt]=node(v,val,head[u]);
	head[u]=ecnt++;
}
void dfs_spfa(int u){
	vis[u]=1;dis[1]=0;
	st[tot++]=1;
	while(tot){
		int u=st[--tot];vis[u]=0;
		for(int i=head[u];i+1;i=edge[i].nxt){
			int v=edge[i].v;
			if(dis[v]>dis[u]+edge[i].val){
				dis[v]=dis[u]+edge[i].val;
				if(vis[v]==0){
					vis[v]=1;
					st[tot++]=v;
				}
			}
		}
	}
}
int main(){
	intt();
	//read(n);read(m);
	scanf("%d%d",&n,&m);
	int a,b,val;
	for(int i=1;i<=m;++i){
		//read(a);read(b);read(val);
		scanf("%d%d%d",&a,&b,&val);
		add(a,b,val);
	}dfs_spfa(1);
	printf("%d\n",dis[n]);
}
 
/*

*/

然后是Dijkstra的:

//#pragma comment(linker, "/STACK:1024000000,1024000000") 
 
#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<fstream>
#include<iostream>  
#include<algorithm>  
using namespace std;  
 
#define ll long long  
#define Pair pair<int,ll>
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// ?? 
//std::ios::sync_with_stdio(false);
const int MAXN=2e5+10;
const int INF32=0x3f3f3f3f;
const ll INF64=0x3f3f3f3f3f3f3f3f;
const ll mod=1e9+7;
const double PI=acos(-1.0); 

//需要自己写文件读入scanf和getchar()都不能用。
//freopen("1.txt","r",stdin);
namespace fastIO {
	#define BUF_SIZE 100000
	//fread -> read
	bool IOerror = 0;
	inline char nc() {
		static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
		if(p1 == pend) {
			p1 = buf;
			pend = buf + fread(buf, 1, BUF_SIZE, stdin);
			if(pend == p1) {
				IOerror = 1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
	}
	inline void read(int &x) {
		char ch;
		while(blank(ch = nc()));
		if(IOerror) return;
		for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
	}
	#undef BUF_SIZE
};
using namespace fastIO;


struct node{
	int v,nxt,val;
	node(int _v=0,int _val=0,int _nxt=0){
		v=_v;val=_val;nxt=_nxt;
	}
}edge[MAXN<<1];
int head[MAXN],ecnt;
int dis[MAXN];
int n,m;

struct cmp{
	bool operator()(const Pair &a,const Pair &b){
		return a.second>b.second;
	}
};
void intt(){
	clean(head,-1);
	ecnt=0;
}
void add(int u,int v,int val){
	edge[ecnt]=node(v,val,head[u]);
	head[u]=ecnt++;
}
void Dijkstr(){
	clean(dis,INF32);dis[1]=0;
	priority_queue<Pair,vector<Pair >,cmp> que;
	que.push({1,0});
	while(que.size()){
		Pair e=que.top();que.pop();
		if(dis[e.first]<e.second)	continue;//小优化 
		for(int i=head[e.first];i+1;i=edge[i].nxt){
			int temp=edge[i].v;
			if(dis[temp]>dis[e.first]+edge[i].val){
				dis[temp]=dis[e.first]+edge[i].val;
				que.push({temp,dis[temp]});
			}
		}
	}
}
int main(){
	intt();
	read(n);read(m);
	//scanf("%d%d",&n,&m);
	int a,b,val;
	for(int i=1;i<=m;++i){
		read(a);read(b);read(val);
		//scanf("%d%d%d",&a,&b,&val);
		add(a,b,val);
	}Dijkstr();
	printf("%d\n",dis[n]);
}
 
/*

*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值