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

第一次写题解。。。。。

Posted by yujingping at 2014-12-05 21:16:00 on Problem 1097
#include<iostream>
#include<queue>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<math.h>
#define MAXN 50
#define INF 2147483647
#define CPY(s,t) memcpy(s,t,sizeof(s))
using namespace std;

struct city {int index;string name;}c[40];//每一座城市的信息
struct query {int a,b;double dist;}s[200];//每一个路牌的信息
struct ans1 {int num;string name;}ans[50];//强迫症让我写这个

inline double abs(double a){if(a>=0)return a;return -a;}
bool cmp(ans1 a,ans1 b){if(a.num<b.num)return true;if(a.num>b.num)return false;return a.name<b.name;}

double dis[MAXN][MAXN];
int n,m,k,q;

void init()
{
	for(int i = 0;i<=35;i++)
	for(int j = 0;j<=35;j++)
	if(i==j)dis[i][j] = 0;
	else dis[i][j] = INF;
	int t1,t2;
	double t3;
	for(int i = 1;i<=m;i++)
	cin>>t1>>t2>>t3,dis[t1][t2] = t3,dis[t2][t1] = t3;
	for(int i = 1;i<=k;i++)
	cin>>c[i].index>>c[i].name;
	cin>>q;
	for(int i = 1;i<=q;i++)
	cin>>s[i].a>>s[i].b>>s[i].dist;
}

void getans(int x)
{
	int t = s[x].a;
	int y = s[x].b;
	int tail = 0;
	for(int i = 1;i<=k;i++)
	{
		if(abs(dis[t][c[i].index] - dis[y][c[i].index] - dis[t][y]) <= 0.000001&&c[i].index!=t)
		{
			tail++;
			ans[tail].num = int(dis[t][c[i].index] - s[x].dist + 0.5);
			ans[tail].name = c[i].name;
		}
	}
	sort(ans+1,ans+1+tail,cmp);
	for(int i = 1;i<=tail;i++)
	{
		cout<<ans[i].name;
		int len = ans[i].name.size();
		for(int j = 1;j<=20-len;j++)
		cout<<' ';
		cout<<ans[i].num<<endl;
	}
}

int main()
{
	while(cin>>n>>m>>k)
	{
		init();
		for(int k1=0;k1<n;k1++)//弗洛伊德算法
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
				if(dis[i][k1]<dis[i][j]-dis[k1][j])
					dis[i][j]=dis[i][k1]+dis[k1][j];
		for(int i = 1;i<=q;i++)
		{
			getans(i);
			cout<<endl;
		}
	}
	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