hdu 1595

OJ

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <list>
#include <queue>
#include <stack>
#include <cmath>

using namespace std;

#define PF(x) (scanf("%d",&x))
#define PT(x,y) (scanf("%d%d",&x,&y))
#define PR(x) (printf("%d\n",x))
#define PRT(x,y)(printf("%d %d\n",x,y))
#define PB(x)(scanf("%I64d",&x))
#define PRB(x)(printf("%I64d\n",(x)))
typedef __int64 LL;
#define N 1505
#define M 105
#define Mod 1000
#define Inf 0x3f3f3f3f

int maps[N][N];
vector<int> G[N];
int n,m;
queue<int> GG;
int ar[N];
int pre[N];
int dist[N];
vector<int> g;

void reinit()
{
	for(int i=0;i<=n;i++)
		if(!G[i].empty()) G[i].clear();
	memset(maps,-1,sizeof(maps));
}

void bfs()
{
	while(!GG.empty()) GG.pop();
	memset(ar,0,sizeof(ar));
    memset(pre,-1,sizeof(pre));
	for(int i=1;i<=n;i++)
		dist[i] = Inf;
	dist[1] = 0;
	ar[1]  = 1;
	GG.push(1);

	while(!GG.empty())
	{
		int t = GG.front();
		GG.pop();

		for(int j=0;j<(int)G[t].size();j++)
		{
			int s = G[t][j];

			if(dist[s]>dist[t]+maps[t][s])
			{
				dist[s] = dist[t] +maps[t][s];
				pre[s] = t;
				if(!ar[s])
				{
					ar[s] = 1;
					GG.push(s);
				}
			}
		}
		ar[t] = 0;
	}
	g.clear();

	int t=n;
	while(true)
	{
		g.push_back(t);
		if(pre[t]==-1) break;
		t = pre[t];
	}
}

int work()
{
	while(!GG.empty()) GG.pop();
	memset(ar,0,sizeof(ar));
	for(int i=1;i<=n;i++)
		dist[i] = Inf;

	dist[1] = 0;
	ar[1] = 1;
	GG.push(1);

	while(!GG.empty())
	{
		int t = GG.front();GG.pop();
		for(int j=0;j<(int)G[t].size();j++)
		{
			int s = G[t][j];
			if(maps[t][s] !=Inf)
			{
				if(dist[s]>dist[t]+maps[t][s])
				{
					dist[s] = dist[t]+maps[t][s];
					if(!ar[s])
					{
						ar[s] = 1;
						GG.push(s);
					}
				}
			}
		}
		ar[t] = 0;
	}
	return dist[n];
}
void init()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		reinit();
		for(int i=0;i<m;i++)
		{
			int x,y,c;
			PT(x,y);PF(c);
			maps[x][y] = c;
			maps[y][x] = c;
			G[x].push_back(y);
			G[y].push_back(x);
		}

		bfs();
		int t = n;
		int mins = 0;

		while(true)//这里刚开始计算少删了一条边,一直不知道哪里错了,WA不计数,无奈一天,只好对着别人代码一一换格式
		{
			int k = maps[pre[t]][t];
			maps[pre[t]][t] = Inf;
			maps[t][pre[t]] = Inf;
            work();
			if(mins<dist[n]) mins = dist[n];
			maps[t][pre[t]] = k;
			maps[pre[t]][t] = k;
			if(pre[t] == 1) break;
			t = pre[t];
		}
		PR(mins);
	}
	return ;
}

int main()
{
	init();
	return 0;
}


你可能感兴趣的:(hdu 1595)