Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求大神帮忙看看思路有什么问题???Orz

Posted by lilintong22222 at 2015-05-17 13:04:15 on Problem 1135
我是把计算的过程直接放在最短路算法中了,拿出来另外计算就是AC的。。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <ctype.h>
#include <vector>
#include <algorithm>
#include <set>
#include <utility>
#include <stack>
#include <queue>
#include <sstream>
#include <cmath>
#include <time.h>
#include <map>

using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
#define repe(i,n) for(int i=1; i<=n; i++)
#define mst(A,k) memset(A,k,sizeof(A))
typedef long long ll;
typedef pair<int,int> Point;
const double eps = 1e-8;
const double INF = 1e15;
const int N = 605;
const int M = 600000;

struct Node{
	int u;
	int t;
	Node(){}
	Node(int u,int t):u(u),t(t){}
	bool operator<(const Node& a)const
	{
		return t > a.t;
	}
};
int n,m;
int fst[N],nxt[M],v[M],w[M],tol;
int vis[N];
int d[N];
int S = 1;
int l,r;
double ans;

void init()
{
	tol = 0;
	mst(fst,-1);
}
void edge(int a,int b,int c)
{
	v[tol] = b;
	w[tol] = c;
	nxt[tol] = fst[a];
	fst[a] = tol++;
}
void bfs()
{
	mst(vis,0);
	repe(i,n) d[i] = INF;
	priority_queue<Node> Q;
	Q.push(Node(S,0));
	d[S] = 0;
	Node p;
	while(!Q.empty())
	{
		p = Q.top();Q.pop();
		if(vis[p.u]) continue;
		vis[p.u] = 1;
		for(int e=fst[p.u]; ~e; e=nxt[e])
		{
			if(vis[v[e]])
			{
				int res = (p.t + d[v[e]] + w[e]);
				if(res*0.5 >= ans)
				{
					ans = res*0.5;
					l = p.u;
					r = v[e];
					if(res == p.t*2) r = -1;
				}
				continue;
			}
			if(p.t + w[e] < d[v[e]])
			{
				d[v[e]] = p.t + w[e];
				Q.push(Node(v[e],d[v[e]]));
			}
		}
	}
}

int main()
{
	int a,b,c,cas = 1;
	while(~scanf("%d%d",&n,&m),n||m)
	{
		init();
		rep(i,m)
		{
			scanf("%d%d%d",&a,&b,&c);
			edge(a,b,c);
			edge(b,a,c);
		}
		ans = 0;
		l = 1;r = -1;
		bfs();
		printf("System #%d\n",cas++);
		if(r == -1)
		{
			printf("The last domino falls after %.1f seconds, at key domino %d.\n",ans,l);
		}
		else
		{
			printf("The last domino falls after %.1f seconds, between key dominoes %d and %d.\n",ans,l,r);
		}
		printf("\n");
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator