poj2983 Is the Information Reliable?
来源:互联网 发布:关我屁事 关你屁事知乎 编辑:程序博客网 时间:2024/04/28 17:07
题中的条件可以整理为两种:A-B=X ,A>B; 即 A-B>=X ,A-B<=X ,A>=B+1;
即为:B<=A-X , A<=B+X , B<=A-1 .这是三个需要满足的条件,那么松弛条件就是 B>A-X , A>B+X , B>A-1 .
类比与dis[v]>dis[u]+w,所以建立的边为edge(A,B,-X),edge(B,A,X),edge(A,B,-1);
因为选择的松弛条件是dis[v]>dis[u]+w,所以如果图中有负权环,就代表关系循环矛盾,就是不可靠,否则就是可靠。
而用spfa判环的话,要考虑的一种情况就是图不连通,而其中一个连通分量中存在负权环,那你如果选择另一个不存在负权环的连通分量中的一个点作为起点,则负权环就不可达,就找不到负权环,所以添加一个超级源点0,对每一个点i建立单向边edge(0,i,0),以这个超级源点作为起点,就能保证每一个连通分量可达,而且不怕坏原有的约束关系。但是不能建立双向边,如果对于每条边都有edge(0,i,0)和edge(i,0,0),则对于原本的A,B就有edge(A,0,0)和edge(0,B,0),相当于AB多了一条路径,就破坏了原来AB的关系。
#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<queue>#include<stack>#include<cstring>#include<map>#include<string>#define LL long long#define clean(a) memset(a,0,sizeof(a))#define maxn 1100using namespace std;struct node{int v,w;};typedef vector<node>vec;vector<vec>head(maxn);int outque[maxn],dis[maxn];bool inque[maxn];int main(){int n,m;while(scanf("%d%d",&n,&m)!=EOF){for(int i=0;i<=n;++i)head[i].clear();for(int i=0;i<m;++i){char op[10];int a,b,w;scanf("%s",op);if(op[0]=='P'){scanf("%d%d%d",&a,&b,&w);head[a].push_back({b,-w});head[b].push_back({a,w});}else{scanf("%d%d",&a,&b);head[a].push_back({b,-1});}}//用spfa判环一定要使图连通,否则添加一个源点,建立源点到各点的通路,//不能从1~n中选一个作为源点加边,给已有的点加边就是破坏约束条件。for(int i=1;i<n;++i){head[0].push_back({i,0});//单向边,保证原图中不连通的点对仍然不连通//head[i].push_back({0,0}); 加上就错了啊,会破坏约束条件,如果原图A,B不连通,就会多出来一条A到B代价为0的条件;}memset(inque,false,sizeof(inque));memset(dis,0x3f,sizeof(dis));memset(outque,0,sizeof(outque));queue<int>que;que.push(0);inque[0]=true;dis[0]=0;bool flag=true;while(!que.empty()){int u=que.front();que.pop();inque[u]=false;outque[u]++;if(outque[u]>n){flag=false;break;}vector<node>& ve=head[u];for(int i=0,len=ve.size();i <len;++i){int v=ve[i].v;int w=ve[i].w;if(dis[v]>dis[u]+w){dis[v]=dis[u]+w;if(!inque[v])que.push(v);}}}if(flag)printf("Reliable\n");elseprintf("Unreliable\n");}}
0 0
- poj2983 Is the Information Reliable?
- poj2983 - Is the Information Reliable?
- POJ2983 Is the Information Reliable?
- poj2983 Is the Information Reliable?
- poj2983 Is the Information Reliable?
- poj2983 Is the Information Reliable
- poj2983 Is the Information Reliable?
- poj2983 Is the Information Reliable?
- 【poj2983】 Is the Information Reliable?
- poj2983 Is the Information Reliable?
- [POJ1201]Intervals && [POJ2983]Is the Information Reliable
- POJ2983——Is the Information Reliable?
- poj2983(差分约束系统)Is the Information Reliable?
- poj2983--Is the Information Reliable?(差分约束)
- Is the Information Reliable? -POJ2983差分约束
- POJ2983 Is the Information Reliable? Candies (查分约束+SPFA)
- [poj2983] Is the Information Reliable? 差分约束系统
- poj2983-Is the Information Reliable?(差分约束判正环)
- 计算机层次化存储结构
- php 四种排序
- 关于面向对象
- Android 点击图片放大
- Activity和进程的关系
- poj2983 Is the Information Reliable?
- vim编辑器常用的命令
- 关于 iOS 10 中 ATS 的问题
- 技术博客之写点什么?
- sql之left join、right join、inner join的区别
- 数据库连接池的简单实现
- ASP.NET连接Oracle数据库方法
- 监控WebSphere相关性能参数
- Draw2d 入门