poj 1860
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 3318 | Accepted: 1014 |
Description
Input
Output
Sample Input
3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00
Sample Output
YES
有一些货币兑换点,兑换有汇率和手续费,要求最后能否增值原有的钱,并且钱种类和增值前是一样的。
可以建立图模型,A-B 有一条边,存储rab 和 cab的值,然后 bellman 算法求能否增值。
代码如下:
#include "iostream" #include "cstdio" #include "cstdlib" #include "cstring" using namespace std; const int MaxN = 110; const double eps = 1e-8; int N,M,S,cnt; double V; typedef struct edge{ int u,v; double r,c; }edge; edge E[1001]; int bellman(){ double d[MaxN]; memset(d,0,sizeof(d)); d[S] = V; while(d[S] <= V + eps){ int f = 0; for(int i = 0 ;i < cnt; ++i){ if(d[E[i].v] + eps < (d[E[i].u] - E[i].c) * E[i].r){ d[E[i].v] = (d[E[i].u] - E[i].c) * E[i].r; f = 1; } } if(!f) return d[S] > V + eps; } return 1; } int main(){ int a,b; double rab,cab,rba,cba; scanf("%d%d%d%lf",&N,&M,&S,&V); cnt = 0; for(int i = 0 ;i < M; ++i){ scanf("%d%d",&a,&b); scanf("%lf%lf%lf%lf",&rab,&cab,&rba,&cba); E[cnt].u = a; E[cnt].v = b; E[cnt].r = rab; E[cnt].c = cab; ++cnt; E[cnt].u = b; E[cnt].v = a; E[cnt].r = rba; E[cnt].c = cba; ++cnt; } if(bellman()) printf("YES/n"); else printf("NO/n"); return 0; }
poj 3259
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 4233 | Accepted: 1463 |
Description
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N , M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.
As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .
To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.
Input
Output
Sample Input
2 3 3 1 1 2 2 1 3 4 2 3 1 3 1 3 3 2 1 1 2 3 2 3 4 3 1 8
Sample Output
NO YES
Hint
和1860几乎一样,用bellman 算法求是否有负环,代码如下:
#include "iostream" #include "cstdio" #include "cstring" #include "cstdlib" using namespace std; const int MaxM = 7000; const int MaxN = 1010; typedef struct edge{ int u,v; int w; }edge; edge e[MaxM]; int F,N,M,W,cnt; void input(){ int a,b,w; cnt = 0; scanf("%d%d%d",&N,&M,&W); for(int i = 0 ;i < M; ++i){ scanf("%d%d%d",&a,&b,&w); e[cnt].u = a; e[cnt].v = b; e[cnt].w = w; ++cnt; e[cnt].u = b; e[cnt].v = a; e[cnt].w = w; ++cnt; } for(int i = 0 ;i < W; ++i){ scanf("%d%d%d",&a,&b,&w); e[cnt].u = a; e[cnt].v = b; e[cnt].w = -w; ++cnt; } } bool bellman(){ int d[MaxN]; memset(d,0,sizeof(d)); d[1] = 0; for(int i = 1 ;i < N; ++i){ for(int j = 0 ;j < cnt; ++j){ if(d[e[j].v] > d[e[j].u] + e[j].w) d[e[j].v] = d[e[j].u] + e[j].w; } } for(int i = 0 ;i < cnt; ++i) if(d[e[i].v] > d[e[i].u] + e[i].w) return false; return true; } int main(){ int a,b,w; scanf("%d",&F); while(F--){ input(); if(bellman()) printf("NO/n"); else printf("YES/n"); } return 0; }
poj 2253
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6059 | Accepted: 2084 |
Description
Input
Output
Sample Input
2 0 0 3 4 3 17 4 19 4 18 5 0
Sample Output
Scenario #1 Frog Distance = 5.000 Scenario #2 Frog Distance = 1.414
Source
可以修改Floyid算法求 D[i,j] = min(D[i,k],D[k,j]) 1 <= k <= j
代码如下:
#include "iostream" #include "cstdio" #include "cstdlib" #include "cstring" #include "cmath" #include "algorithm" #define sqr(x) ((x)*(x)) using namespace std; const int MaxN = 500; const double Inf = 1e30; typedef struct point{ double x,y; }point; point P[MaxN]; double map[MaxN][MaxN]; int N; void input(){ for(int i = 0 ;i < N; ++i) scanf("%lf%lf",&P[i].x,&P[i].y); } double dis(const point& a,const point& b){ return sqrt(sqr(a.x -b.x) + sqr(a.y - b.y)); } void calc(){ for(int i = 0 ;i < N; ++i){ for(int j = 0; j < N; ++j) map[i][j] = dis(P[i],P[j]); } } void work(){ for(int k = 0 ;k < N; ++k){ for(int i = 0 ;i < N; ++i){ for(int j = 0 ;j < N; ++j){ if(max(map[i][k],map[k][j]) < map[i][j]) map[i][j] = max(map[i][k],map[k][j]); } } } } int main(){ int t = 1; while(scanf("%d",&N) && N){ input(); calc(); work(); printf("Scenario #%d/n",t); printf("Frog Distance = %.3f/n",map[0][1]); printf("/n"); ++t; } return 0; }
poj 3026
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1807 | Accepted: 572 |
Description
Input
Output
Sample Input
2
6 5
#####
#A#A##
# # A#
#S ##
#####
7 7
#####
#AAA###
# A#
# S ###
# #
#AAA###
#####
Sample Output
8 11
现对每个 ‘A’ 和 ‘S’ 的点 BFS 求得他与其他‘A’ ‘S’点的距离,然后从S 求 Minmum Spanning Tree 把所有
代价加起来,就是答案
代码如下:
#include "iostream" #include "cstdio" #include "cstdlib" #include "algorithm" #include "cstring" using namespace std; const int MaxN = 110; const int INF = 0x7fffffff; char map[MaxN][MaxN]; int g[MaxN][MaxN]; int graph[110][110]; int X,Y,num; int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}}; typedef struct point{ int x,y; void init(int _x,int _y){ x = _x; y = _y; } }point; point q[MaxN * MaxN]; int input(){ int cnt = 2; char tmp[1000]; scanf("%d%d",&X,&Y); gets(tmp); for(int i = 0 ;i < Y; ++i){ gets(tmp); strcpy(map[i],tmp); for(int j = 0 ;j < X; ++j){ if(tmp[j] == ' ') g[i][j] = 0; else if(tmp[j] == '#') g[i][j] = -1; else if(tmp[j] == 'S') g[i][j] = 1; else g[i][j] = cnt++; } } return cnt-1; } void bfs(int x,int y){ int s,e; int dis[MaxN][MaxN]; bool vis[MaxN][MaxN]; for(int i = 0 ;i < MaxN; ++i) for(int j = 0 ;j < MaxN; ++j) vis[i][j] = false; vis[x][y] = true; dis[x][y] = 0; point tmp; tmp.init(x,y); s = e = 0; q[e++] = tmp; int a,b,na,nb; while(s != e){ tmp = q[s++]; a = tmp.x; b = tmp.y; //printf("a = %d b = %d/n",a,b); //getchar(); for(int i = 0 ;i < 4; ++i){ na = a + dir[i][0]; nb = b + dir[i][1]; if(na >= 0 && na < Y && nb >= 0 && nb < X && g[na][nb] >= 0 && !vis[na][nb]){ vis[na][nb] = true; dis[na][nb] = dis[a][b] + 1; tmp.init(na,nb); q[e++] = tmp; if(g[na][nb] > 0){ graph[g[x][y]][g[na][nb]] = dis[na][nb]; graph[g[na][nb]][g[x][y]] = dis[na][nb]; } } } } } int prime(){ int ret = 0; int dis[MaxN]; bool vis[MaxN]; memset(vis,false,sizeof(vis)); for(int i = 0 ;i < MaxN; ++i) dis[i] = INF; dis[1] = 0; for(int i = 1;i <= num; ++i){ int tmin = INF; int pos; for(int j = 1 ;j <= num; ++j){ if(!vis[j] && dis[j] < tmin){ tmin = dis[j]; pos = j; } } vis[pos] = true; ret += dis[pos]; for(int j = 1; j <= num; ++j){ if(!vis[j] && graph[pos][j] != INF && graph[pos][j] < dis[j]) dis[j] = graph[pos][j]; } } return ret; } int main(){ int T; scanf("%d",&T); while(T--){ for(int i =0 ;i < MaxN; ++i) for(int j = 0 ;j < MaxN; ++j) g[i][j] = 0; num = input(); for(int i = 0 ;i <= num; ++i) for(int j = 0 ;j <= num; ++j) graph[i][j] = INF; for(int i = 0 ;i < Y; ++i) for(int j = 0 ;j < X; ++j) if(g[i][j] > 0) bfs(i,j); /*for(int i = 1;i <= num; ++i){ for(int j = 1; j<= num; ++j){ printf("%d ",graph[i][j]); } putchar('/n'); }*/ printf("%d/n",prime()); } return 0; }