USTC 1280 / 携程决赛1004 最短路径的代价

来源:互联网 发布:office for 破解mac 编辑:程序博客网 时间:2024/05/18 01:25

好像有的朋友看不到题。在这里贴一下题目。

这道题就是USTC   的1280   这是题目链接,大家做出来可以在这里交代码:点击进入



最短路径的代价

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)

Total Submission(s) : 31   Accepted Submission(s) : 14

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

通常情况下找到N个节点、M 条边的无向图中某两点的最短路径很简单。假设每条边都有各自的长度以及删除这条边的代价,那么给出两个点X和Y,
删除一些边可以使得X到Y的最短路径增加,求删除边最少的代价。

Input

第一行包含参数N和M,分别表示节点和边的个数(2 ≤N≤ 1000,1 ≤M≤ 11000 )
第二行包含参数X和Y,表示源点和终点( 1 ≤ X,Y ≤ N,X ≠ Y);
剩下M行每行包含4个参数Ui、Vi、Wi和Ci,分别表示节点Ui和Vi之间边的长度Wi以及删除的代价Ci ( 1 ≤ Ui , Vi ≤ N, 0 ≤Wi, Ci ≤1000 );
如果某行N=M=0就表示输入结束。

Output

对于每个用例,按行输出增加X和Y直接最短路径所需要的最小代价。这个代价是所有删除的边的代价之和。

Sample Input

2 31 21 2 2 31 2 2 41 2 3 54 51 41 2 1 12 3 1 13 4 1 11 4 3 22 2 2 34 52 31 2 3 22 4 3 41 3 2 33 4 2 31 4 0 10 0

Sample Output

736

Source

CodingTrip - 携程编程大赛 (决赛)


解题思路:
这道题主要的思路是先求最短路,把所有的最短路在图中抠出来然后建一条以C为容量的容量网络。然后求
最小割也就是最大流。挺简单的,当时题目数据范围有坑,WA了好几次。

下面是代码:
#include <iostream>#include <algorithm>#include <cstdlib>#include <cstdio>#include <cstring>#include <queue>#include <cmath>#include <stack>//#pragma comment(linker, "/STACK:1024000000");#define EPS (1e-8)#define unsignedBigInteger unsigned long long int#define _LL __int64#define _INF 0x3f3f3f3f#define INF 1<<29#define Mod 1000000007#define BigInteger long longusing namespace std;int min(int a,int b){    if(a>b)a=b;    return a;}int max(int a,int b){    if(a<b)a=b;    return a;}struct node{    int to,w,c,next;} edge1[22005];int head1[2005],dis[2005],deep[2005];struct node edge3[22005];int head3[2005],cnt3;int n,m,x,y,cnt1;bool vis[2005];void addedge(int u,int v,int w,int c){    edge1[cnt1].to=v;    edge1[cnt1].w=w;    edge1[cnt1].c=c;    edge1[cnt1].next=head1[u];    head1[u]=cnt1++;}void spfa(int ans){    queue <int>q;    q.push(ans);    vis[ans]=true;    dis[ans]=0;    while(!q.empty())    {        int p,t=q.front();        q.pop();        p=head1[t];        vis[t]=false;        while(p!=-1)        {            if(dis[edge1[p].to]>dis[t]+edge1[p].w)            {                dis[edge1[p].to]=dis[t]+edge1[p].w;                if(!vis[edge1[p].to])                {                    vis[edge1[p].to]=true;                    q.push(edge1[p].to);                }            }            p=edge1[p].next;        }    }}bool bfs(int x,int y){    memset(deep,-1,sizeof(deep));    queue <int > q;    q.push(x);    deep[x]=0;    while(!q.empty())    {        int t=q.front();        q.pop();        int p=head3[t];        while(p!=-1)        {            int v=edge3[p].to;            if( deep[v]==-1&&edge3[p].w>0)            {                q.push(v);                deep[v]=deep[t]+1;            }            p=edge3[p].next;        }    }    return deep[y]!=-1;}int dfs(int src,int flow,int y){    if(src==y)return flow;    int sum=0;    int p=head3[src];    while(p!=-1)    {        int v=edge3[p].to;        if(deep[v]==deep[src]+1&&edge3[p].w>0)        {            int tmp=dfs(v,min(flow-sum,edge3[p].w),y);            sum+=tmp;            edge3[p].w-=tmp;            edge3[p^1].w+=tmp;            if(flow-sum==0)break;        }        p=edge3[p].next;    }    return sum;}int dinic(int x,int y){    int ans=0;    while(bfs(x,y))    {        ans+=dfs(x,INF,y);    }    return ans;}void addedge3(int u,int v,int w){    edge3[cnt3].to=v;    edge3[cnt3].w=w;    edge3[cnt3].next=head3[u];    head3[u]=cnt3++;    edge3[cnt3].to=u;    edge3[cnt3].w=0;    edge3[cnt3].next=head3[v];    head3[v]=cnt3++;}void bfs1(int x,int y){    memset(deep,0,sizeof(deep));    queue <int > q;    q.push(x);    deep[x]=1;    while(!q.empty())    {        int t=q.front();        q.pop();        if(t==y)continue;        int p=head1[t];        while(p!=-1)        {            if(dis[edge1[p].to]-dis[t]==edge1[p].w)            {                int v=edge1[p].to;                addedge3(t,v,edge1[p].c);                if(deep[v]==0)                {                    deep[v]=1;                    q.push(v);                }            }            p=edge1[p].next;        }    }}int main(){    int u,v,w,c,case1=1;    while(scanf("%d%d",&n,&m),n||m)    {        scanf("%d%d",&x,&y);        for(int i=0; i<=n; i++)        {            dis[i]=INF;            vis[i]=false;            head1[i]=-1;            head3[i]=-1;        }        cnt1=0;        cnt3=0;        for(int i=0; i<m; i++)        {            scanf("%d%d%d%d",&u,&v,&w,&c);            addedge(u,v,w,c);            addedge(v,u,w,c);        }        spfa(x);        //printf("%d\n",dis[y]);        bfs1(x,y);        printf("%d\n",dinic(x,y));    }    return 0;}


0 0
原创粉丝点击