Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
1A 哈希+bellman ford#include <algorithm> #include <iostream> #include <sstream> #include <cstdlib> #include <cstring> #include <cstdio> #include <string> #include <bitset> #include <vector> #include <queue> #include <stack> #include <cmath> #include <list> #include <map> #include <set> using namespace std; /***************************************/ #define ll long long #define int64 __int64 /***************************************/ const int INF = 0x7f7f7f7f; const double eps = 1e-8; const double PIE=acos(-1.0); const int dx[]= {0,-1,0,1}; const int dy[]= {1,0,-1,0}; const int fx[]= {-1,-1,-1,0,0,1,1,1}; const int fy[]= {-1,0,1,-1,1,-1,0,1}; /***************************************/ void openfile() { freopen("data.in","rb",stdin); freopen("data.out","wb",stdout); } /**********************华丽丽的分割线,以上为模板部分*****************/ const int MAX=-1; #define N 1000 int t,edgenum; double w; typedef struct Edge { int u,v; double cost; }; Edge edge[N]; double dis[N]; bool Bellman_ford() { int i,j; for(i=1;i<=t;i++) dis[i]=MAX; dis[1]=1; for(i=1;i<=t-1;i++) for(j=1;j<=edgenum;j++) if (dis[edge[j].v]<dis[edge[j].u]*edge[j].cost) dis[edge[j].v]=dis[edge[j].u]*edge[j].cost; int flag=0; for(i=1;i<=edgenum;i++) if (dis[edge[i].v]<dis[edge[i].u]*edge[i].cost) { flag=1; break; } return flag; } int main() { int cas=0; while(scanf("%d",&t)&&t) { map<string,int>mp; char a[50],b[50]; int i,j; int num=0; for(i=0;i<t;i++) scanf("%s",a); scanf("%d",&edgenum); for(i=1;i<=edgenum;i++) { scanf("%s %lf %s",a,&w,b); if (mp[a]==0) mp[a]=++num; if (mp[b]==0) mp[b]=++num; edge[i].u=mp[a]; edge[i].v=mp[b]; edge[i].cost=w; } if (Bellman_ford()) printf("Case %d: Yes\n",++cas); else printf("Case %d: No\n",++cas); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator