poj 3259
来源:互联网 发布:高斯键盘知乎 编辑:程序博客网 时间:2024/04/30 11:54
还是找带负权的环路
没找到自己写的问题在哪。。。
转的AC的http://blog.csdn.net/xsbailong/article/details/6864693
邻接矩阵对于松弛效率比较低,应该用边的结构体
//用邻接矩阵的一种实现,效率很低。。#include <iostream>#include <cstdio>#include <string.h>#include <limits.h>using namespace std;int map[510][510];int dist[510];void bellman( int n ){ int i,j,k; bool in; memset( dist,0,sizeof(dist) ); for( i=1;i<=n;i++ ) { in=false; for( j=1;j<=n;j++ ) for( k=1;k<=n;k++ ) { if( map[j][k]!=0 ) { if( dist[j]!=INT_MAX && dist[k]>dist[j]+map[j][k] ) dist[k]=dist[j]+map[j][k] , in=true ; } } if( !in ) break; } if( i==n+1 ) printf( "YES\n" ); else printf( "NO\n" );}int main(){ int T,n,m,w,i,j; int a,b,value; scanf( "%d",&T ); while( T-- ) { memset( map,0,sizeof(map) ); scanf( "%d%d%d",&n,&m,&w ); for( i=0;i<m;i++ ) { scanf( "%d%d%d",&a,&b,&value ); if( map[a][b]==0 || map[a][b]>value ) { map[a][b]=value; map[b][a]=map[a][b]; } } for( i=0;i<w;i++ ) { scanf( "%d%d%d",&a,&b,&value ); if( map[a][b]==0 || map[a][b]>-value ) map[a][b]=-value; } bellman(n); } return 0;}
我的怎么就不对呢。。。WAWAWA
#include <iostream>#include <sstream>#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <vector>#include <set>#include <cctype>#include <algorithm>#include <cmath>#include <deque>#include <queue>#include <map>#include <queue>#include <list>#include <iomanip>using namespace std; //////#define INF 20000#define maxn 2550#define max(a,b)(a>b?a:b)///int N, M, W;int maze[maxn][maxn];int dist[maxn];bool bellman(){int i, j, k;int lines;//总的边数lines = N;//初始化从0出发的最短距离数组distdist[1] = 0;for (i = 2; i <= N; i++){if (maze[1][i] != 0)dist[i] = maze[1][i];}//进行边数次松弛操作bool flag;for (i = 0; i < lines; i++){flag = false;for (j = 1; j <= N; j++){for (k = 1; k <= N; k++){if (maze[j][k] != 0){if (dist[j] != INF && dist[k] > dist[j] + maze[j][k]){dist[k] = dist[j] + maze[j][k];flag = true;}}}}if (!flag)break;}/*for (j = 1; j <= N; j++){for (k = 1; k <= N; k++){if (maze[j][k] != 0){if (dist[k] > dist[j] + maze[j][k])return true;}}}*/if (i == lines + 1)return true;elsereturn false;}int main(){freopen("F:\\input.txt","r",stdin );///int i, j;int cases;cin >> cases;while (cases--){memset(maze, 0, sizeof(maze));memset(dist, INF, sizeof(dist));cin >> N >> M >> W;//双向路径for (i = 0; i < M; i++){int from, to ,weight;cin >> from >> to >> weight;maze[from][to] = weight;maze[to][from] = weight;}//单向负边路径for (i = 0; i < W; i++){int from, to, weight;cin >> from >> to >> weight;maze[from][to] = -weight;}if (bellman())cout << "YES" << endl;elsecout << "NO" << endl;}/// return 0;}
- poj 3259
- poj 3259
- poj-3259
- poj 3259
- poj 3259
- poj 3259
- POJ 3259
- poj 3259
- poj.3259
- poj-3259
- POJ 3259
- poj-3259
- poj 3259
- POJ-3259
- poj 3259
- POJ 3259
- poj----3259
- poj 3259
- 【菜鸟C++学习笔记】26.运算符重载
- 关于在其他机器上重启管理服务的步骤
- 编程体会
- Ubuntu快捷键 终端快捷键
- [ VIM ] vim 打造自己的VIM界面!
- poj 3259
- 马戏团人塔问题
- Draggable 可拖动
- 知识交易如何才能做到“安全”“诚信”?
- vb.net 2010/2008 导出到excel,无需引用
- CPU-Z v1.62
- 使用_TrackMouseEvent捕获WM_MOUSELEAVE和WM_MOUSEHOVER消息
- 谁是最好的Coder
- opengl编程指南