题干:
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
Line 1: A single integer, F. F farm descriptions follow.
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 Tseconds 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
Lines 1.. F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).
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 1, FJ cannot travel back in time.
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.
题目大意:
有若干个虫洞,给出了若干普通路径和其所用时间以及虫洞的路径和其倒回的时间,现问你能否回到出发之前的时间,注意普通路径是双向的,虫洞是单向的。
解题报告:
由题目所给信息已经可以构建一张完整的图了,然后进一步理解题目的意思其实是这张图是否存在负环,因此使用Bellman_Ford或者spfa即可。
AC代码:(邻接表储存图)(266ms)
using namespace std;
int n,cnt;
const int MAX = 505;
const int INF = 0x3f3f3f3f;
int dis[MAX],maze[MAX][MAX],cntt[MAX],head[MAX];
bool vis[MAX];
struct Edge {
int to,w,ne;
Edge(){}//没有此构造函数不能写 node t 这样
Edge(int to,int w,int ne):to(to),w(w),ne(ne){}//可以写node(pos,cost)这样
} e[200000 + 5];//数组别开小了
void add(int u,int v,int w) {
e[cnt].to = v;
e[cnt].w = w;
e[cnt].ne = head[u];
head[u] = cnt++;
}
bool spfa(int s){
dis[s]=0; vis[s]=1; //队列初始化,s为起点
int v;
queue<int > q;
q.push(s);
while (!q.empty()){ //队列非空
v=q.front(); //取队首元素
q.pop();
vis[v]=0; //释放队首结点,因为这节点可能下次用来松弛其它节点,重新入队
for(int i=head[v]; i!=-1; i=e[i].ne) //对所有顶点
{
if (dis[e[i].to]>dis[v]+e[i].w) {
dis[e[i].to] = dis[v]+e[i].w; //修改最短路
if (vis[e[i].to]==0) { //如果扩展结点i不在队列中,入队
cntt[e[i].to]++;
vis[e[i].to]=1;
q.push(e[i].to);
if(cntt[e[i].to] >=n) return true;
}
}
}
}
return false;
}
void init() {
cnt = 0;
memset(vis,0,sizeof(vis));
memset(maze,0,sizeof(maze));
memset(e,0,sizeof(e));
memset(dis,INF,sizeof(dis));
memset(cntt,0,sizeof(cntt));
memset(head,-1,sizeof(head));
}
int main()
{
int t;
int M,W,u,v,w;
cin>>t;
while(t--) {
init();
scanf("%d%d%d",&n,&M,&W);
for(int i = 1; i<=M; i++) {
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);add(v,u,w);
}
for(int i = 1; i<=W; i++) {
scanf("%d%d%d",&u,&v,&w);
add(u,v,-w);
}
cntt[1] =1;
if(spfa(1)) printf("YES\n");
else printf("NO\n");
}
return 0 ;
}
AC代码2:(邻接矩阵)(需要判重边!)(1079ms)
using namespace std;
int n;
const int MAX = 505;
const int INF = 0x3f3f3f3f;
int dis[MAX],maze[MAX][MAX],cnt[MAX];
bool vis[MAX];
bool spfa(int s){
// for(int i=0; i<=n; i++) dis[i]=99999999; //初始化每点i到s的距离
dis[s]=0; vis[s]=1; //队列初始化,s为起点
int i, v;
queue<int > q;
q.push(s);
while (!q.empty()){ //队列非空
v=q.front(); //取队首元素
q.pop();
vis[v]=0; //释放队首结点,因为这节点可能下次用来松弛其它节点,重新入队
for(i=1; i<=n; i++) //对所有顶点
if (maze[v][i]!=INF && dis[i]>dis[v]+maze[v][i]){
dis[i] = dis[v]+maze[v][i]; //修改最短路
if (vis[i]==0){ //如果扩展结点i不在队列中,入队
cnt[i]++;
vis[i]=1;
q.push(i);
if(cnt[i] >=n) return true;
}
}
}
return false;
}
void init() {
memset(vis,0,sizeof(vis));
memset(maze,INF,sizeof(maze));
memset(dis,INF,sizeof(dis));
memset(cnt,0,sizeof(cnt));
}
int main()
{
int t;
int M,W,u,v,w;
cin>>t;
while(t--) {
init();
scanf("%d%d%d",&n,&M,&W);
for(int i = 1; i<=M; i++) {
scanf("%d%d%d",&u,&v,&w);
if(w<maze[u][v])
maze[u][v] = maze[v][u] = w;
}
for(int i = 1; i<=W; i++) {
scanf("%d%d%d",&u,&v,&w);
maze[u][v] = -w;
}
cnt[1] =1;
if(spfa(1)) printf("YES\n");
else printf("NO\n");
}
return 0 ;
}
AC代码3:Bellman_Ford算法(125ms)
//Bellman_Ford算法试试
using namespace std;
int t , n , m, w;
int dis[MAXN];
struct Edge{
int x;
int y;
int value;
}e[MAXN];
bool judge(){
for(int i = 0 ; i < m*2+w ; i++){
if(dis[e[i].y] > dis[e[i].x] + e[i].value)
return false;
}
return true;
}
void Bellman_Ford(){
dis[1] = 0;
for(int i = 2 ; i <= n ; i++)
dis[i] = INF;
for(int i = 1 ; i <= n ; i++){
for(int j = 0 ; j < m*2+w; j++){
if(dis[e[j].y] > dis[e[j].x] + e[j].value)
dis[e[j].y] = dis[e[j].x] + e[j].value;
}
}
if(judge())
printf("NO\n");
else
printf("YES\n");
}
int main()
{
int uu,vv,ww,i;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&w);
for(i=0;i<m*2;)
{
scanf("%d%d%d",&uu,&vv,&ww);
e[i].x=uu;
e[i].y=vv;
e[i++].value=ww;
e[i].x=vv;
e[i].y=uu;
e[i++].value=ww;
}
for(;i<m*2+w;i++)
{
scanf("%d%d%d",&uu,&vv,&ww);
e[i].x=uu;
e[i].y=vv;
e[i].value=-ww;
}
Bellman_Ford();
}
return 0;
}
总结:
1.脑残了吧调了一下午+一晚上bug尝试了邻接矩阵邻接表,发现是写了memset(dis,INF,sizeof(INF));这一行乐色?呵呵
2.如果用邻接表储存这题别忘了去重一下不然会wa的,至于为什么权值为负的时候不需要判重,大概是因为这题只要是负的,都不影响结果?因为这题要看的是,是否存在负环啊。