您的位置:首页 > 其它

hdu 1595 find the longest of the shortest

2012-01-12 02:05 274 查看
要求去除一条路后的最短路的最大长度~~~~挺有意思的~~我的方法很简单,先求出任何一条路都没去时的最短路,那么去掉路后能使最短路变长的路一定在没去时的最短路的集合里面!

这样复杂度直接下降到 O(v*e) ,ac轻而易举~

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
using namespace std;
const int maxn=1111;
const int inf=0x3f3f3f3f;
struct zz
{   
    int from;
    int to;
    int cost;
    int id;
}zx,tz;
vector<zz>g[maxn];
vector<int>v;
queue<int>q;
int go[maxn];
int back[maxn];
int id[maxn];
bool vis[maxn];
int n,m,tmax;

void spfa()
{
    memset(vis,false,sizeof(vis));
    memset(back,0,sizeof(back));  
    memset(id,0,sizeof(id));
    for(int i=1;i<=n;i++)
    {
        go[i]=inf;
    }  
    while(!q.empty())
    {
        q.pop();
    }
    go[1]=0;
    q.push(1);
    vis[1]=true;
    int now,to,cost;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0;i<g[now].size();i++)
        {
            to=g[now][i].to;
            cost = g[now][i].cost+go[now];
            if(cost < go[to] )
            {     
                go[to]=cost;
                back[to]=now;
                id[to]=g[now][i].id;
                if(!vis[to])
                {
                    vis[to]=true;
                    q.push(to);
                }
            }              
        }   
        vis[now]=false; 
    }
    int temp=n;
    while(back[temp])      
    {                        
        v.push_back(id[temp]);  
        temp=back[temp]; 
    }
    return ;    
}
int spfa1(int tid)
{
    int temp,now,to,cost,t;
    while(!q.empty())
    {
        q.pop();    
    }    
    memset(vis,false,sizeof(vis));
    for(int i=1;i<=n;i++)
    {
        go[i]=inf;
    }
    go[1]=0;
    q.push(1);
    vis[1]=true;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0;i<g[now].size();i++)
        {
            if(g[now][i].id == tid)
            {
                continue;
            }
            to=g[now][i].to;
            cost=g[now][i].cost + go[now];
            if(cost<go[to])
            {
                go[to]=cost;
                if(!vis[to])
                {       
                    vis[to]=true;
                    q.push(to);
                }
            }
        }
        vis[now]=false;
    }    
    return go
;
}
int spfa2()
{
    tmax=0;
    int temp;
    for(int i=0;i<v.size();i++)
    {
        temp=spfa1(v[i]);
        if(temp > tmax )
        {
            tmax=temp;
        }            
    }   
    return tmax; 
}

int main()
{
    while(cin>>n>>m)
    {
        for(int i=0;i<=n;i++)   
        {
            g[i].clear();
        }
        v.clear();
        for(int i=1;i<=m;i++)
        {
            cin>>zx.from>>zx.to>>zx.cost;
            zx.id=i;
            g[zx.from].push_back(zx);
            swap(zx.from,zx.to);
            g[zx.from].push_back(zx);       
        }
        spfa();
        cout<<spfa2()<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: