比赛 noip2016普及练习1 评测结果 AAAAAAAAA
题目名称 回家 最终得分 100
用户昵称 小e 运行时间 0.007 s
代码语言 C++ 内存使用 0.55 MiB
提交时间 2016-11-03 19:08:34
显示代码纯文本
#include "cstdio"
#include "cstring"
#include "queue"
using namespace std;

int P;
struct Edge
{
	int to, w, next;
}edges[21000];
int head[130], tot;
int dis[130];
bool pushed[130];
deque <int> Q;

inline void AddEdge(const int& from, const int& to, const int& w)
{
	edges[++tot].to = to;
	edges[tot].w = w;
	edges[tot].next = head[from];
	head[from] = tot;
}

inline void SPFA(const int& s)
{
	char ans1;
	int ans2;
	memset(dis, 0x3f, sizeof(dis));
	memset(pushed, 0, sizeof(pushed));
	dis[s] = 0;
	Q.push_front(s); pushed[s] = 1;
	while(!Q.empty()){
		int front = Q.front();
		Q.pop_front(); pushed[front] = 0;
		for(int i = head[front]; i; i = edges[i].next){
			int to = edges[i].to, w = edges[i].w;
			if(dis[to] > dis[front] + w){
				dis[to] = dis[front] + w;
				if(!pushed[to]){
					if(Q.empty() || dis[to] < dis[Q.front()]) Q.push_front(to);
					else Q.push_back(to);
					pushed[to] = 1;
				}
			}
		}
	}
	int minn = 0x3f3f3f3f;
	for(int i = 1; i <= (int)'z'; i++){
		if(i >= (int)'A' && i < int('Z') && dis[i] < minn){
			minn = dis[i];
			ans1 = (char)i;
			ans2 = dis[i];
		}
	}
	printf("%c %d\n", ans1, ans2);
}

#define SUBMIT
int main(int argc, char const *argv[])
{
#ifdef SUBMIT
	freopen("comehome.in", "r", stdin); freopen("comehome.out", "w", stdout);
#endif	

	scanf("%d", &P);
	char from[5], to[5];
	int w;
	for(int i = 1; i <= P; i++){
		scanf("%s%s%d", from, to, &w);
		AddEdge((int)from[0], (int)to[0], w);
		AddEdge((int)to[0], (int)from[0], w);
	}
	SPFA((int)'Z');

#ifndef SUBMIT
	printf("\n----------\n");
	getchar(); getchar();
#else
	fclose(stdin); fclose(stdout);
#endif

	return 0;
}