POJ-2983 Is the Information Reliable?

Posted on   Capterlliar  阅读(18)  评论(0编辑  收藏  举报

题意:给出两种约束,P代表x在y北边z光年,V代表x至少在y北边1光年,求下列信息是否矛盾。

解:令x-y>=z为x至少在y北边z光年,相等的时候加一条x-y<=z,也就是y-x>=-z,建图判环即可。T了一发,因为n只有1000,我非要初始化1e6个点。。。

代码:

复制代码
 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <queue>
 4 using namespace std;
 5 #define maxx 1000005
 6 #define inf 1e8+10
 7 #define ll long long
 8 int n,m;
 9 struct edge{
10     int u,v,w;
11     int nxt;
12 }e[maxx*2];
13 int cnt=0,head[maxx]={0};
14 int vis[maxx],dis[maxx],in[maxx];
15 void add(int u,int v,int w){
16     e[++cnt].u=u;
17     e[cnt].v=v;
18     e[cnt].w=w;
19     e[cnt].nxt=head[u];
20     head[u]=cnt;
21 }
22 int spfa(int x){
23     for(int i=0;i<n+10;i++)
24         dis[i]=-inf;
25     memset(vis,0,sizeof vis);
26     memset(in,0,sizeof in);
27     queue<int> q;
28     dis[x]=0;in[x]++;
29     q.push(x);
30     while(!q.empty()){
31         int now=q.front();
32         q.pop();
33         vis[now]=0;
34         for(int i=head[now];i;i=e[i].nxt){
35             int to=e[i].v;
36             int temp=dis[now]+e[i].w;
37             if(temp>dis[to]){
38                 dis[to]=temp;
39                 in[to]++;
40                 if(in[to]>=n+1)
41                     return 0;
42                 if(!vis[to]) {
43                     q.push(to);
44                     vis[to] = 1;
45                 }
46             }
47         }
48     }
49     return 1;
50 }
51 signed main(){
52     while(~scanf("%d%d",&n,&m)) {
53         memset(e,0,sizeof e);
54         memset(head,0,sizeof head);
55         cnt=0;
56         for (int i = 0; i < m; i++) {
57             char opt[2];
58             scanf("%s", opt);
59             if (opt[0] == 'P') {
60                 int x, y, z;
61                 scanf("%d%d%d", &x, &y, &z);
62                 add(x, y, z);
63                 add(y, x, -z);
64             } else if (opt[0] == 'V') {
65                 int x, y;
66                 scanf("%d%d", &x, &y);
67                 add(x, y, 1);
68             }
69         }
70         int s = n + 1;
71         for (int i = 1; i <= n; i++)
72             add(s, i, 0);
73         if (spfa(s))
74             printf("Reliable\n");
75         else
76             printf("Unreliable\n");
77     }
78     return 0;
79 }
View Code
复制代码

 

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示