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

注意是从1到n而不是求所有的点。哭了,看错题目。

Posted by ym94 at 2019-07-25 09:17:00 on Problem 1797
AC代码
#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
#define Max 1010
#define min(a,b) a>b?b:a;
#define max(a,b) a>b?a:b;
#define inf 0x3f3f3f3f
using namespace std;
bool vis[Max];
int dist[Max];
int max_num;  //ans
struct Node{
	int next;
	int to;
	int dis;
}edge[Max*Max];
int num_edge;
int head[Max];
void add_edge(int x,int y,int w)
{
	edge[++num_edge].next=head[x];
	edge[num_edge].to=y;
	edge[num_edge].dis=w;
	head[x]=num_edge;
}
struct heap{
	int data;
	int weight;
};
struct cmp{ //维护最大的那条边 
	bool operator()(struct heap a,struct heap b)
	{
		return a.weight<b.weight;
	}
};
priority_queue <struct heap,vector<heap>, cmp> q;
void dijkstra(int num)
{
	dist[num]=inf;
	while(q.size()) q.pop();
	struct heap h;
	h.data=num;
	h.weight=inf; //因为最小的会影响 
	q.push(h);
	while(q.size())
	{
		int mmax=inf,u=-1;
		h=q.top();
		q.pop();
		mmax=h.weight;
		u=h.data;
		if(vis[u])
			continue;
		vis[u]=true;
		for(int i=head[u];i;i=edge[i].next)
		{
			if(edge[i].dis>dist[edge[i].to]&&mmax>dist[edge[i].to])
			{
				dist[edge[i].to]=min(mmax,edge[i].dis);
				if(vis[edge[i].to])
					continue;
				h.data=edge[i].to;
				h.weight=dist[edge[i].to];
				q.push(h);
			}
		}
	 }
}
int main()
{
	int t,n,m,x,y,w;
	scanf("%d",&t);
	for(int k=1;k<=t;k++)
	{
		memset(head,0,sizeof(head));
		memset(vis,false,sizeof(vis));
		memset(dist,0,sizeof(dist));
		num_edge=0;
		scanf("%d%d",&n,&m);
		while(m--)
		{
			scanf("%d%d%d",&x,&y,&w);
			if(y==x) continue;
			add_edge(x,y,w);
			add_edge(y,x,w);
		}
		max_num=inf;
		dijkstra(1);
//		for(int i=2;i<=n;i++)
//			max_num=min(max_num,dist[i]);
		printf("Scenario #%d:\n%d\n\n",k,dist[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