bzoj 1731/hdu 3592(差分约束)
来源:互联网 发布:淘宝官方 编辑:程序博客网 时间:2024/05/29 03:29
传送门
题解:
定向之后(小编号连大编号),”≥”连(b,a,-c),”≤”连(a,b,c),跑最短路
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>using namespace std;const int MAXN=1002,MAXM=20004,INF=0x3f3f3f3f;int kase;int n,X,Y,a,b,c;int head[MAXN],edge;struct EDGE { int v,nxt,w;}e[MAXM];int dis[MAXN],in[MAXN];bool vis[MAXN];void adde(int u,int v,int w) { e[++edge].nxt=head[u],e[edge].v=v,e[edge].w=w,head[u]=edge;}inline int spfa(int source,int sink) { queue<int> q; memset(in,0,sizeof(in)); memset(vis,false,sizeof(vis)); memset(dis,INF,sizeof(dis)); q.push(1),vis[1]=true,dis[1]=0,in[1]=1; while (!q.empty()) { int p=q.front(); q.pop(); vis[p]=false; for (int i=head[p];~i;i=e[i].nxt) { int v=e[i].v; if (dis[v]>dis[p]+e[i].w) { dis[v]=dis[p]+e[i].w; if (!vis[v]) { vis[v]=true; q.push(v); if (++in[v]>n) return -1; } } } } if (dis[sink]==INF) return -2; return dis[sink];}int main() {// freopen("hdu 3592.in","r",stdin); scanf("%d",&kase); while (kase--) { edge=0; memset(head,-1,sizeof(head)); scanf("%d%d%d",&n,&X,&Y); for (int i=0;i<X;++i) { scanf("%d%d%d",&a,&b,&c); if (a>b) a^=b^=a^=b; adde(a,b,c); } for (int i=0;i<Y;++i) { scanf("%d%d%d",&a,&b,&c); if (a>b) a^=b^=a^=b; adde(b,a,-c); } printf("%d\n",spfa(1,n)); } return 0;}
阅读全文
0 0
- bzoj 1731/hdu 3592(差分约束)
- hdu 3592 差分约束
- hdu 3592(差分约束)
- bzoj 2330(差分约束)
- 【bzoj 2330】糖果(差分约束)
- BZOJ 1077 天平 (差分约束)
- bzoj 2788(差分约束+tarjan)
- POJ 3169 Layout (HDU 3592) 差分约束
- hdu 3592 World Exhibition(差分约束,最短路)
- HDU 3592 World Exhibition(差分约束)
- HDU--3592[World Exhibition] 差分约束
- hdu 3592 World Exhibition //差分约束
- hdu 3592 World Exhibition 差分约束
- HDU 3592 World Exhibition 差分约束
- hdu-3592-World Exhibition-差分约束
- |hdu 3592|差分约束|World Exhibition
- hdu 3592 差分约束 first bolld
- HDU 1384 Intervals(差分约束)
- IIC介绍(四)之从机介绍
- 【数据结构之线性表总结】
- 数据结构实验之链表三:链表的逆置
- vtk单窗口显示多个源数据代码
- jsp三大指令
- bzoj 1731/hdu 3592(差分约束)
- javascript之数据类型、数据、变量、内存、对象、函数
- SpringMVC总结以及在面试中的一些问题
- 破解Navicat for MySQL
- BZOJ 2809 [Apio2012]dispatching dfs序+左偏树
- 关于markdown window10系统安装报错
- OkHttp深入理解(2)RetryAndFollowUpInterceptor
- 离线赛20171008总结
- 初识XML