POJ 3159 Candies
来源:互联网 发布:美的面试 知乎 编辑:程序博客网 时间:2024/06/05 16:37
题目来源
题意:给N个人分水果,一共M组数据。每组数据包含A、B、C。B的糖果数-A的糖果数<=C。最后求N比1最多多多少糖果。
注意:存边一定要用前向星。
(1)spfa+优先队列
(2)dijkstra优先队列
#include <iostream>#include <stdio.h>#include <string.h>#include <queue>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <math.h>#define inf 0x3f3f3f3f#define ll long long#define mod 99991#define N 51000using namespace std;int n,m;struct node{ int to; int w; int next;}e[150010];int head[30010];int dis[31000];bool vis[31000];int Top;void init(){ memset(head,-1,sizeof(head)); Top = 0;}void add(int u,int v,int w){ e[Top].to = v; e[Top].w = w; e[Top].next = head[u]; head[u] = Top++;}void spfa(){ memset(dis,inf,sizeof(dis)); memset(vis,false,sizeof(vis)); priority_queue<int>q; q.push(1); dis[1] = 0; vis[1] = true; while(!q.empty()){ int u = q.top(); q.pop(); vis[u] = false; for(int i = head[u]; ~i; i = e[i].next){ int v = e[i].to; int w = e[i].w; if(dis[v] > dis[u]+w){ dis[v] = dis[u]+w; if(!vis[v]){ q.push(v); vis[v] = true; } } } } printf("%d\n",dis[n]);}int main(){ int a,b,c; while(~scanf("%d%d",&n,&m)){ init(); while(m--){ scanf("%d%d%d",&a,&b,&c); add(a,b,c); } spfa(); } return 0;}
#include <iostream>#include <stdio.h>#include <string.h>#include <queue>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <math.h>#define inf 0x3f3f3f3f#define ll long long#define mod 99991#define N 51000using namespace std;int n,m;struct node{ int to; int w; int next; bool operator < (const node &b)const{ return w > b.w; }}now,next,e[150001];int head[31000];int dis[31000];bool vis[31000];int Top = 0;void add(int u,int v,int w){ e[Top].to = v; e[Top].w = w; e[Top].next = head[u]; head[u] = Top++;}void dijkstra(){ memset(dis,inf,sizeof(dis)); memset(vis,false,sizeof(vis)); priority_queue<node>q; dis[1] = 0; now.to = 1; now.w = 0; q.push(now); while(!q.empty()){ now = q.top(); q.pop(); int u = now.to; if(vis[u])continue; vis[u] = true; for(int i = head[u]; ~i; i=e[i].next){ int v = e[i].to; int w = e[i].w; if(dis[v] > dis[u]+w){ dis[v] = dis[u]+w; next.to = v; next.w = dis[v]; q.push(next); } } } printf("%d\n",dis[n]);}int main(){ int a,b,c; while(~scanf("%d%d",&n,&m)){ memset(head,-1,sizeof(head)); Top = 0; while(m--){ scanf("%d%d%d",&a,&b,&c); add(a,b,c); } dijkstra(); } return 0;}
0 0
- poj 3159 Candies
- POJ 3159 Candies
- poj 3159 Candies
- POJ 3159 candies总结
- POJ 3159 Candies
- POJ 3159 Candies
- poj 3159-Candies
- POJ-3159-Candies
- POJ 3159 Candies
- POJ 3159 Candies
- poj 3159 Candies
- POJ 3159 Candies
- poj 3159 Candies
- POJ -- 3159 Candies
- poj 3159 Candies
- Candies ( POJ 3159)
- poj 3159 Candies
- POJ 3159 Candies
- 数组去掉重复值、删除元素、添加元素
- SingleAtomicCase运行
- leetcode:单链表之Reverse Linked List II
- js中 日期天数加1
- spring注解的详细说明
- POJ 3159 Candies
- openstack 工作流workflows使用原理讲解
- Category 命名冲突检测
- 网络编程学习笔记一:Socket编程
- hdu 5791 two 多校联赛第五场
- JVM基础知识
- 使用RecyclerView和Glide实现图片瀑布流(防止错位)
- android studio启动时关闭上次打开的项目
- POST 提交XML 文件