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 |
诡异的题目下面的代码感觉是对的,结果是错的。(后面还有代码感觉是错的,结果是对的) # include <stdio.h> # include <stdlib.h> # define MAXVALUE 0x7fffffff; int dist[1000]; typedef struct p{ int s,t; int weight; }Path; Path path[10000]; int numField = 0; int numPath = 0; bool bell_man(int x){ for(int i = 1; i <= numField; i ++){ dist[i] = MAXVALUE; } dist[x] = 0; for(int k = 0; k < numField; k ++) for(int i = 1; i <= numPath; i ++){ int s = path[i].s; int t = path[i].t; if(dist[s] + path[i].weight < dist[t]){ dist[t] = dist[s] + path[i].weight; } } for(int i = 1; i <= numPath; i ++){ int s = path[i].s,t = path[i].t; if(dist[s] + path[i].weight < dist[t]) return true; } return false; } int main(){ int t; scanf("%d",&t); while(t--){ int n1,n2; scanf("%d %d %d",&numField,&n1,&n2); numPath = 1; for(int i = 0; i < n1; i ++){ scanf("%d %d %d",&path[numPath].s,&path[numPath].t,&path[numPath].weight); numPath ++; path[numPath].s = path[numPath-1].t; path[numPath].t = path[numPath-1].s; path[numPath].weight = path[numPath-1].weight; numPath ++; } for(int i = 0; i < n2; i++){ scanf("%d %d %d",&path[numPath].s,&path[numPath].t,&path[numPath].weight); path[numPath].weight *= -1; numPath ++; } numPath --; bool flag = false; for(int i =1 ; i <= numField; i ++) if(bell_man(i)){ printf("YES\n"); flag = true; break; } if(!flag) printf("NO\n"); } return 0; } 下面的代码感觉是错的,结果是对的。 #include <iostream> using namespace std; int value[1000]; struct e { int from, to; int time; }edge[25000]; int main() { int f; cin >> f; while (f--) { int n, m, w; cin >> n >> m >> w; int i,j; int from, to, time; int total = 1; for (i = 1; i <= m; ++i) { cin >> from >> to >> time; edge[total].time = time; edge[total].from = from; edge[total].to = to; ++total; edge[total].time = time; edge[total].from = to; edge[total].to = from; ++total; } for (i = 1; i <= w; ++i) { cin >> from >> to >> time; edge[total].time = -time; edge[total].from = from; edge[total].to = to; ++total; } //memset(value,0,sizeof(value)); bool flag = true; for (i = 0; i < n; ++i) { for (j = 0; j < total; ++j) if (value[edge[j].to] > value[edge[j].from] + edge[j].time) { value[edge[j].to] = value[edge[j].from] + edge[j].time; } } for (i = 0; i < total; ++i) if (value[edge[i].to] > value[edge[i].from] + edge[i].time) { flag = false; break; } if (flag) cout << "NO" << endl; else cout << "YES" << endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator