清北暑假模拟day2 之

 

/*
现场代码,枚举每条边删除
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector> 
#include<queue>
#include<algorithm>
#define mx 1005

using namespace std;
struct orz
{
    int d,p;
    friend bool operator <(orz a,orz b) {return a.d>b.d;}
};
struct Edge{
    int to;
    int w;
    int id;
};
priority_queue < orz > ss;
int flag,cnt,v[mx],d[mx],n,m,l;
vector<Edge> edge[mx];
bool vis[mx];
void input(){
    cin>>n>>l;
    int u,v,wei;
    Edge test;
    for(int i = 1;i <= l;i++){
        cin>>u>>v>>wei;
        if(u == v) continue;
        test.id = ++cnt;
        test.to = v;
        test.w = wei;
        edge[u].push_back(test);
        test.to = u;
        edge[v].push_back(test);
    }
    m = 1;
}
void dij(int s)
{
    for(int i = 0;i < mx;i++) d[i] = 1000000000;
    d[s]=0;
    orz tmp;
    tmp.d=0,tmp.p=s;
    ss.push(tmp);
    flag++;
    int x,dd;
    Edge j;
    while (!ss.empty())
    {
        tmp=ss.top();
        ss.pop();
        x=tmp.p,dd=tmp.d;
        if (v[x]==flag) continue;
        v[x]=flag;
        for (int i = 0;i < edge[x].size();i++){
            if(vis[edge[x][i].id]) continue;
            j = edge[x][i];
            if (d[j.to]>dd+j.w)
            {
                d[j.to]=dd+j.w;
                tmp.d=dd+j.w,tmp.p=j.to;
                ss.push(tmp);
            }
        }

    }
}
int main(){
    freopen("di.in","r",stdin);
    freopen("di.out","w",stdout);
    input();
    int ans = 0;
    for(int i = 1;i <= cnt;i++){
        vis[i] = true;
        dij(1);
        vis[i] = false;
        if(d[n] < 1000000000) ans = max(ans,d[n]);
    }
    cout<<ans;
    return 0;
}
/*
要使最短路改变,就只能删除最短路经过的边
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

using namespace std;

const int maxn=210;
const int maxm=50010;

int n,m,en,f[maxn],q[maxn],dist[maxn],edg[maxm][3],z[maxn];

bool use[maxn];

struct edge
{
    int e,d,id;
    edge *next;
}*v[maxn],ed[maxm<<1],*fe[maxn];

void add_edge(int id,int s,int e,int d)
{
    en++;
    ed[en].next=v[s];v[s]=ed+en;v[s]->e=e;v[s]->d=d;v[s]->id=id;
}

void spfa()
{
    int front=0,tail=1;
    memset(dist,0x3f,sizeof(dist));
    dist[1]=0;
    use[1]=true;
    q[0]=1;
    for (;front!=tail;)
    {
        int now=q[front++];
        if (front==maxn) front=0;
        use[now]=false;
        for (edge *e=v[now];e;e=e->next)
            if (dist[e->e]>dist[now]+e->d)
            {
                dist[e->e]=dist[now]+e->d;
                f[e->e]=now;fe[e->e]=e;
                if (!use[e->e])
                {
                    use[e->e]=true;
                    q[tail++]=e->e;
                    if (tail==maxn) tail=0;
                }
            }
    }
}

int main()
{
    freopen("di.in","r",stdin);
    freopen("di.out","w",stdout);

    scanf("%d%d",&n,&m);
    for (int a=1;a<=m;a++)
        scanf("%d%d%d",&edg[a][0],&edg[a][1],&edg[a][2]);
    for (int a=1;a<=m;a++)
    {
        add_edge(a,edg[a][0],edg[a][1],edg[a][2]);
        add_edge(a,edg[a][1],edg[a][0],edg[a][2]);
    }
    spfa();
    int cnt=0;
    for (int a=n;a!=1;a=f[a])
        z[++cnt]=fe[a]->id;
    int ans=-1;
    for (int a=1;a<=cnt;a++)
    {
        en=0;
        memset(v,0,sizeof(v));
        for (int b=1;b<=m;b++)
            if (b!=z[a])
            {
                add_edge(b,edg[b][0],edg[b][1],edg[b][2]);
                add_edge(b,edg[b][1],edg[b][0],edg[b][2]);
            }
        spfa();
        if (dist[n]!=0x3f3f3f3f) ans=max(ans,dist[n]);
    }
    printf("%d\n",ans);

    return 0;
}

 








posted @ 2016-10-19 19:13  ACforever  阅读(158)  评论(0编辑  收藏  举报