差分约束 [1](poj 3159)(简单)
来源:互联网 发布:工厂方法模式java 编辑:程序博客网 时间:2024/04/28 18:19
差分约束:其实就是解一组三角形不等式 A+c<=B;可通过建图求最短路求得;
这道题比较裸,正常建完图后求一次最短路就ok;就是在用spfa时不能用队列,会超时,拿一个数组模拟就行;
#include <iostream>
#include <cstring>#include <cstdio>
#include <cmath>
using namespace std;
#define oo 1<<30
#define M 155555
#define N 33333
int n,num;
bool vis[N];
int dis[N],head[N];int q[M];
struct node{
int u,v,next,c;
}edge[M];
void addedge(int a,int b,int c)
{
edge[num].u=a;
edge[num].v=b;
edge[num].c=c;
edge[num].next=head[a];
head[a]=num++;
}
void spfa()
{
int x,y,d;
int top=0;
q[top++]=1;
memset(vis,0,sizeof(vis));
for(int i=0;i<=n;i++)
dis[i]=oo;
dis[1]=0;
vis[1]=1;
while(top!=0)
{
x=q[--top];
vis[x]=0;
for(int i=head[x];i!=-1;i=edge[i].next)
{
y=edge[i].v;
d=edge[i].c;
if(dis[x]+d<dis[y])
{
dis[y]=dis[x]+d;
if(!vis[y])
{
q[top++]=y;
vis[y]=1;
}
}
}
}
}
int main()
{ int m,a,b,c;
while(cin>>n>>m)
{
num=0;
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&a,&b,&c);
addedge(a,b,c);
}
spfa();
cout<<dis[n]<<endl;
}
}
0 0
- 差分约束 [1](poj 3159)(简单)
- poj--3159--Candies(简单差分约束)
- poj 3159 Candies(差分约束)
- POJ 3159Candies (差分约束)
- POJ 3159 (差分约束+spfa)
- POJ 3159 Candies (差分约束)
- poj 3159 Candies(差分约束)
- POJ 3159 Candies (差分约束#1)
- poj 3159 差分约束
- POJ 3159 差分约束
- poj 3159 差分约束
- poj--3169--Layout(简单差分约束)
- poj 3159 Candies(差分约束 spfa stack实现)
- poj 3159 差分约束(分发糖果)
- POJ 3159 Candies(SPFA+栈)差分约束
- POJ 3159 Candies(spfa最短路,差分约束)
- POJ 题目3159 Candies(差分约束)
- POJ - 3159 Candies(差分约束系统)
- 利用SecureCRT进行文件的传输
- js中我定义一个var变量,在页面想显示出来,用什么东西显示比较好,var比较长还有空格
- CentOS7的安装、配置
- Visual Studio 2015速递(3)——ASP.NET 新特性
- 阿里面试经历JAVA总结
- 差分约束 [1](poj 3159)(简单)
- Thread.currentThread().getContextClassLoader()与Test.class.getClassLoader()区别
- 数据产品初期需求思考的问题
- Android--利用ViewFlipper实现屏幕切换动画效果
- 不论我们爱或者不爱,我们为什么选择SAP?
- 设计模式-单例模式
- Eclipse IDE 最好使快捷键
- 简单的Java打字游戏
- 1001.害死人不偿命的(3n+1)猜想 (15)