跟随kai巨巨,开始了寒假的刷题计划,简直刷新了我对刷题的看法!
每个专题,题目很多,我只能以自己的水平,把能写出来的 记一记、、、
B Wormholes POJ 3259
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 toF (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
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2.. M+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2.. M+ W+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
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
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.
题意:输入一个数F,接下来有F组数据, 输入三个数n, m, w。n 代表点的个数,m 代表双向路径的对数,且权值为正,w 代表单向路径的对数,权值为负。最后要求的就是,判断是否存在负环,存在则输出YES, 不存在就输出NO
可以用bellman - ford 来做,用的kuangbin 的模版 ,模版有错- -# 搞了半天、、
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 5500;
int dist[MAXN];
int n, m;
struct Edge
{
int u, v;
int cost;
Edge(int _u = 0, int _v = 0, int _cost = 0): u(_u), v(_v), cost(_cost){}
};
vector<Edge> E;
bool bellman_ford(int start)
{
for(int i = 1; i <= n; i ++)
dist[i] = INF;
dist[start] = 0;
for(int i = 1; i < n; i ++)
{
bool flag = false;
for(int j = 0; j < E.size(); j ++)
{
int u = E[j].u;
int v = E[j].v;
int cost = E[j].cost;
if(dist[v] > dist[u] + cost)
{
dist[v] = dist[u] + cost;
flag = true;
}
}
if(!flag)
break;
}
for(int j = 0; j < E.size(); j ++)
{
if(dist[E[j].v] > dist[E[j].u] + E[j].cost)
return true;
}
return false;
}
int main()
{
int T;
int w, s, e, t;
while(~scanf("%d",&T))
{
while(T --)
{
while(!E.empty())
{
E.pop_back();
}
scanf("%d%d%d",&n,&m,&w);
for(int i = 1; i <= m ;i ++)
{
scanf("%d%d%d",&s, &e, &t);
E.push_back((Edge){s, e, t});
E.push_back((Edge){e, s, t});
}
for(int j = 1; j <= w; j ++)
{
scanf("%d%d%d",&s,&e,&t);
E.push_back((Edge){s, e, -t});
}
/* for(int i = 0; i < E.size(); i ++)
{
printf("%d %d %d\n", E[i].u, E[i].v, E[i].cost);
}*/
if(bellman_ford(1))
{
printf("YES\n");
}
else
printf("NO\n");
}
}
}
F POJ 2240
Description
Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.
Input
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.
Output
Sample Input
3 USDollar BritishPound FrenchFranc 3 USDollar 0.5 BritishPound BritishPound 10.0 FrenchFranc FrenchFranc 0.21 USDollar 3 USDollar BritishPound FrenchFranc 6 USDollar 0.5 BritishPound USDollar 4.9 FrenchFranc BritishPound 10.0 FrenchFranc BritishPound 1.99 USDollar FrenchFranc 0.09 BritishPound FrenchFranc 0.19 USDollar 0
Sample Output
Case 1: Yes Case 2: No
floyd 的 一个变种
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <string.h>
using namespace std;
#define N 110
map <string, int> mp;
double tt[N][N];
int n, m;
bool floyd()
{
for( int k = 1; k <= n; k ++)
for( int i = 1; i <= n; i ++)
for( int j = 1; j <= n; j ++)
if( tt[i][j] < tt[i][k] * tt[k][j])
tt[i][j] = tt[i][k] * tt[k][j];
for( int i = 1; i <= n; i ++)
if( tt[i][i] > 1)
return true;
return false;
}
int main()
{
double a;
char t[110];
char t1[110];
char t2[110];
int icase = 1;
while(~scanf("%d",&n),n)
{
memset(tt, 0, sizeof(tt));
for(int i = 1; i <= n; i ++)
{
scanf("%s",t);
mp[t] = i;
}
scanf("%d",&m);
for(int i = 1; i <= m; i ++)
{
scanf("%s%lf%s", t1, &a, t2);
tt[ mp[t1] ][ mp[t2] ] = a;
}
printf("Case %d: ", icase ++);
if(floyd())
printf("Yes\n");
else
printf("No\n");
}
}
E POJ 1125
Description
Unfortunately for you, stockbrokers only trust information coming from their "Trusted sources" This means you have to take into account the structure of their contacts when starting a rumour. It takes a certain amount of time for a specific stockbroker to pass the rumour on to each of his colleagues. Your task will be to write a program that tells you which stockbroker to choose as your starting point for the rumour, as well as the time it will take for the rumour to spread throughout the stockbroker community. This duration is measured as the time needed for the last person to receive the information.
Input
Each person is numbered 1 through to the number of stockbrokers. The time taken to pass the message on will be between 1 and 10 minutes (inclusive), and the number of contacts will range between 0 and one less than the number of stockbrokers. The number of stockbrokers will range from 1 to 100. The input is terminated by a set of stockbrokers containing 0 (zero) people.
Output
It is possible that your program will receive a network of connections that excludes some persons, i.e. some people may be unreachable. If your program detects such a broken network, simply output the message "disjoint". Note that the time taken to pass the message from person A to person B is not necessarily the same as the time taken to pass it from B to A, if such transmission is possible at all.
Sample Input
3 2 2 4 3 5 2 1 2 3 6 2 1 2 2 2 5 3 4 4 2 8 5 3 1 5 8 4 1 6 4 10 2 7 5 2 0 2 2 5 1 5 0
Sample Output
3 2 3 10
感觉怪怪的,有点问题、、
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int INF = 0xffffff;
int map[150][150];
int dis[150];
int vis[150];
int n;
int dij(int sour)
{
int i, j, k;
int t;
for(i = 1; i <= n; i ++)
{
vis[i] = 0;
dis[i] = map[sour][i];
}
vis[sour] = 1;
int min;
for(i = 1; i < n; i ++)
{
min = INF;
t = 0;
for(j = 1; j <= n; j ++)
{
if(!vis[j] && min > dis[j])
{
t = j;
min = dis[j];
}
}
vis[t] = 1;
for(j = 1; j <= n; j ++)
{
int newdis = min + map[t][j];
if(!vis[j] && dis[j] > newdis)
{
dis[j] = newdis;
}
}
}
/*for(int i = 1; i <= n; i ++)
{
if(vis[i] == 0)
return -1;
}*/
return min;
}
void init()
{
for(int i = 0 ; i <= n; i ++)
{
for(int j = 0; j <= n; j ++)
{
map[i][j] = INF;
}
}
}
int main()
{
int a, b;
while(~scanf("%d",&n), n)
{
init();
for(int j = 1; j <= n; j ++)
{
scanf("%d",&a);
for(int i = 1; i <= a; i ++)
{
scanf("%d",&b);
scanf("%d",&map[j][b]);
}
}
bool flag = false;
int minn = dij(1);
int ans = 1;
for(int i = 2; i <= n; i ++)
{
int tmp_1 = dij(i);
if(tmp_1 == -1)
{
printf("disjoint\n");
break;
flag = true;
}
if(tmp_1 < minn)
{
minn = tmp_1;
ans = i;
}
}
if(flag)
continue;
printf("%d %d\n",ans, minn);
}
return 0;
}