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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值