程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu5294||2015多校聯合第一場1007 最短路+最大流

hdu5294||2015多校聯合第一場1007 最短路+最大流

編輯:C++入門知識

hdu5294||2015多校聯合第一場1007 最短路+最大流


 

 

Problem Description Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the entrance of the tomb while Dumb Zhang’s at the end of it. The tomb is made up of many chambers, the total number is N. And there are M channels connecting the chambers. Innocent Wu wants to catch up Dumb Zhang to find out the answers of some questions, however, it’s Dumb Zhang’s intention to keep Innocent Wu in the dark, to do which he has to stop Innocent Wu from getting him. Only via the original shortest ways from the entrance to the end of the tomb costs the minimum time, and that’s the only chance Innocent Wu can catch Dumb Zhang.
Unfortunately, Dumb Zhang masters the art of becoming invisible(奇門遁甲) and tricks devices of this tomb, he can cut off the connections between chambers by using them. Dumb Zhang wanders how many channels at least he has to cut to stop Innocent Wu. And Innocent Wu wants to know after how many channels at most Dumb Zhang cut off Innocent Wu still has the chance to catch Dumb Zhang.

Input There are multiple test cases. Please process till EOF.
For each case,the first line must includes two integers, N(<=2000), M(<=60000). N is the total number of the chambers, M is the total number of the channels.
In the following M lines, every line must includes three numbers, and use ai、bi、li as channel i connecting chamber ai and bi(1<=ai,bi<=n), it costs li(0 The entrance of the tomb is at the chamber one, the end of tomb is at the chamber N.

Output Output two numbers to stand for the answers of Dumb Zhang and Innocent Wu’s questions.
Sample Input
8 9
1 2 2
2 3 2
2 4 1
3 5 3
4 5 4
5 8 1
1 6 2
6 7 5
7 8 1

Sample Output
2 6

Source 2015 Multi-University Training Contest 1
/**
hdu5294 最短路+最大流
題目大意:給定一個無向圖,從起點到終點,只有走最短路,才能在規定時限內到達,問最少去掉幾條邊使不能到達,最多去掉幾條邊仍能到達
解題思路:http://blog.sina.com.cn/s/blog_15139f1a10102vnx5.html 和官方題解想的一樣
*/
#include
#include
#include
#include
using namespace std;
const int oo=1e9;
const int mm=161111;
const int mn=2330;
int node,src,dest,edge;
int ver[mm],flow[mm],_next[mm];
int head[mn],work[mn],dis[mn],q[mn];

void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0; i<=node; ++i)head[i]=-1;
    edge=0;
}

void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,_next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,_next[edge]=head[v],head[v]=edge++;
}

bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0; i=0; i=_next[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int &i=work[u],v,tmp; i>=0; i=_next[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0; i que;
int dis1[maxn],dis2[maxn];

void addedge1(int u,int v,int c)
{
    edges[edge1].w=c,edges[edge1].to=v,edges[edge1].next=head1[u],head1[u]=edge1++;
}

void init()
{
    memset(head1,-1,sizeof(head1));
    edge1=0;
}

void spfa(int s,int n)//單源最短路(s為起點,n為節點總數)
{
    int u;
    for (int i=0; i<=n; i++)
        dis1[i]=INF;
    memset(vis,0,sizeof(vis));
    while (!que.empty()) que.pop();
    que.push(s);
    vis[s]=true;
    dis1[s]=0;
    while (!que.empty())
    {
        u=que.front();
        que.pop();
        vis[u]=false;
        for (int i=head1[u]; i!=-1; i=edges[i].next)
        {
            int v=edges[i].to;
            int w=edges[i].w;
            if (dis1[v]>dis1[u]+w)
            {
                dis1[v]=dis1[u]+w;
                if (!vis[v])
                {
                    vis[v]=true;
                    que.push(v);
                }
            }
        }
    }
}
////========================================
int aa[60080][3],bb[60080][3];
int main()
{
    int n,m;
    while(~scanf(%d%d,&n,&m))
    {
        init();
        for(int i=0;i%d
,dis2[n]);
        spfa(n,n);
       // printf(dis1->%d
,dis1[1]);
        int k=0;
        for(int i=0;idis2[aa[i][1]])
                swap(aa[i][0],aa[i][1]);
           // printf(n-%d:%d-1 %d %d %d
,aa[i][1],aa[i][0],dis1[aa[i][1]],aa[i][2],dis2[aa[i][0]]);
            if(dis1[aa[i][1]]+aa[i][2]+dis2[aa[i][0]]==dis2[n])
            {
                bb[k][0]=aa[i][0];
                bb[k++][1]=aa[i][1];
               // printf(%d %d
,bb[k-1][0],bb[k-1][1]);
            }
        }
        prepare(n+1,1,n);
        for(int i=0;i

 

Problem Description Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the entrance of the tomb while Dumb Zhang’s at the end of it. The tomb is made up of many chambers, the total number is N. And there are M channels connecting the chambers. Innocent Wu wants to catch up Dumb Zhang to find out the answers of some questions, however, it’s Dumb Zhang’s intention to keep Innocent Wu in the dark, to do which he has to stop Innocent Wu from getting him. Only via the original shortest ways from the entrance to the end of the tomb costs the minimum time, and that’s the only chance Innocent Wu can catch Dumb Zhang. Unfortunately, Dumb Zhang masters the art of becoming invisible(奇門遁甲) and tricks devices of this tomb, he can cut off the connections between chambers by using them. Dumb Zhang wanders how many channels at least he has to cut to stop Innocent Wu. And Innocent Wu wants to know after how many channels at most Dumb Zhang cut off Innocent Wu still has the chance to catch Dumb Zhang. Input There are multiple test cases. Please process till EOF. For each case,the first line must includes two integers, N(<=2000), M(<=60000). N is the total number of the chambers, M is the total number of the channels. In the following M lines, every line must includes three numbers, and use ai、bi、li as channel i connecting chamber ai and bi(1<=ai,bi<=n), it costs li(0 The entrance of the tomb is at the chamber one, the end of tomb is at the chamber N. Output Output two numbers to stand for the answers of Dumb Zhang and Innocent Wu’s questions. Sample Input
8 9
1 2 2
2 3 2
2 4 1
3 5 3
4 5 4
5 8 1
1 6 2
6 7 5
7 8 1

Sample Output
2 6

Source 2015 Multi-University Training Contest 1

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved