Codeforces Round #287 (Div. 2) E. Breaking Good 最短路

原创 2015年07月10日 10:21:24

E. Breaking Good
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Breaking Good is a new video game which a lot of gamers want to have. There is a certain level in the game that is really difficult even for experienced gamers.

Walter William, the main character of the game, wants to join a gang called Los Hermanos (The Brothers). The gang controls the whole country which consists of n cities with m bidirectional roads connecting them. There is no road is connecting a city to itself and for any two cities there is at most one road between them. The country is connected, in the other words, it is possible to reach any city from any other city using the given roads.

The roads aren’t all working. There are some roads which need some more work to be performed to be completely functioning.

The gang is going to rob a bank! The bank is located in city 1. As usual, the hardest part is to escape to their headquarters where the police can’t get them. The gang’s headquarters is in city n. To gain the gang’s trust, Walter is in charge of this operation, so he came up with a smart plan.

First of all the path which they are going to use on their way back from city 1 to their headquarters n must be as short as possible, since it is important to finish operation as fast as possible.

Then, gang has to blow up all other roads in country that don’t lay on this path, in order to prevent any police reinforcements. In case of non-working road, they don’t have to blow up it as it is already malfunctional.

If the chosen path has some roads that doesn’t work they’ll have to repair those roads before the operation.

Walter discovered that there was a lot of paths that satisfied the condition of being shortest possible so he decided to choose among them a path that minimizes the total number of affected roads (both roads that have to be blown up and roads to be repaired).

Can you help Walter complete his task and gain the gang’s trust?

Input
The first line of input contains two integers n, m (2 ≤ n ≤ 105, ), the number of cities and number of roads respectively.

In following m lines there are descriptions of roads. Each description consists of three integers x, y, z (1 ≤ x, y ≤ n, ) meaning that there is a road connecting cities number x and y. If z = 1, this road is working, otherwise it is not.

Output
In the first line output one integer k, the minimum possible number of roads affected by gang.

In the following k lines output three integers describing roads that should be affected. Each line should contain three integers x, y, z (1 ≤ x, y ≤ n, ), cities connected by a road and the new state of a road. z = 1 indicates that the road between cities x and y should be repaired and z = 0 means that road should be blown up.

You may output roads in any order. Each affected road should appear exactly once. You may output cities connected by a single road in any order. If you output a road, it’s original state should be different from z.

After performing all operations accroding to your plan, there should remain working only roads lying on some certain shortest past between city 1 and n.

If there are multiple optimal answers output any.

Sample test(s)
input
2 1
1 2 0
output
1
1 2 1
input
4 4
1 2 1
1 3 0
2 3 1
3 4 1
output
3
1 2 0
1 3 1
2 3 0
input
8 9
1 2 0
8 3 0
2 3 1
1 4 1
8 7 0
1 5 1
4 6 1
5 7 0
6 8 0
output
3
2 3 0
1 5 0
6 8 1
Note
In the first test the only path is 1 - 2

In the second test the only shortest path is 1 - 3 - 4

In the third test there are multiple shortest paths but the optimal is 1 - 4 - 6 - 8
题意是,要找从1到n的最短路,如果有多条最短路,则要求这条路径上的权值最小,每条路的权值是1 或 0,这里因为每条路长度都为1,所以就用了dij求最短路的算法,使用优先队列优化!

#define INF         9000000000
#define EPS         (double)1e-9
#define mod         1000000007
#define PI          3.14159265358979
//*******************************************************************************/
#endif
#define N 100050
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int n,m,x,y,z,plast[N],ans[N],ansi,vis[N];
struct edge{
    int s,e,w,f;
    edge(){
        f = 0;
    }
    edge(int ss,int ee,int ww){
        s = ss;e = ee;w = ww; f = 0;
    }
};
vector<int> p[N];
map<pii,int> mymap;
edge edges[N + N];
struct node{
   int v,c,last,s;
   node(){
   }
   node(int vv,int cc,int l,int ss){
        v = vv;c = cc;last = l;s = ss;
   }
   bool operator < (const node t) const{
      return v>t.v || (v == t.v && c < t.c);
   }
};
priority_queue<node> Q;
void Dij(){
    Q.push(node(0,0,-1,0));
    while(!Q.empty()){
        node top = Q.top();Q.pop();
        if(vis[top.s]) continue;
        vis[top.s] = true;
        plast[top.s] = top.last;
        if(top.s == n-1){
            ansi = 0;
            int s = top.s;
            while(s != -1){
                ans[ansi++] = s;
                s = plast[s];
            }
            for(int k = ansi-1;k>=1;k--){
                edges[mymap[pii(ans[k-1],ans[k])]].f = 1;
                edges[mymap[pii(ans[k-1],ans[k])]^1].f = 1;
            }
            ansi = 0;
            for(int k =0;k<m+m + 2;k=k+2){
                if(edges[k].f  || edges[k ^ 1].f ){
                    edges[k].f = edges[k ^ 1].f = 1;

                    if(edges[k].w == 0)
                    ans[ansi++] = k,edges[k].w = 1;
                }
                else {
                    if(edges[k].w == 1)
                        ans[ansi++] = k,edges[k].w = 0;
                }
            }
            printf("%d\n",ansi);
            for(int k=0;k<ansi;k++){
                printf("%d %d %d\n",edges[ans[k]].s + 1,edges[ans[k]].e + 1,edges[ans[k]].w);
            }
            return ;
        }
        FI(p[top.s].size()){
            Q.push(node(top.v + 1,top.c + edges[p[top.s][i]].w,top.s,edges[p[top.s][i]].e));
        }
    }
}
int main()
{
    while(S2(n,m)!=EOF)
    {
        FI(n)p[i].clear();
        mymap.clear();
        memset(plast,-1,sizeof(plast));
        memset(vis,false,sizeof(vis));
        while(!Q.empty())Q.pop();
        FI(m){
            S2(x,y);S(z);x--,y--;
            edges[2 * i].s = x;edges[2 * i].e = y;edges[2 * i].w = z;edges[2 * i].f = 0;
            edges[2 * i + 1].s = y;edges[2 * i + 1].e = x;edges[2 * i + 1].w = z;edges[2 * i+1].f = 0;
            p[x].push_back(2 * i);p[y].push_back(2 * i+1);
            mymap[pii(x,y)] = i + i;mymap[pii(y,x)] = i + i+1;
        }
        Dij();
    }
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Codeforces Round #287 (Div. 2) E. Breaking Good(最短路、dp)

题意: N≤105个城市,M≤105条边,保证无向图连通且无自环和重边N\le10^5个城市, M\le10^5条边, 保证无向图连通且无自环和重边 边分2种,0代表毁坏的,1代表正常的边分...
  • lwt36
  • lwt36
  • 2016年02月13日 17:24
  • 254

Codeforces Round #287 (Div. 2) 507E E. Breaking Good

题目链接:http://codeforces.com/contest/507/problem/E 题目大意: 在一个有n个城市m条边的国家,有一个犯罪团伙。这个犯罪团伙想要抢银行。犯罪团伙基地在城...

Codeforces 507E Breaking Good【最短路SPFA+Dp+记录路径】好题~~~

E. Breaking Good time limit per test 2 seconds memory limit per test 256 megabytes input ...

Codeforces Round #Pi (Div. 2) E. President and Roads (最短路+强连通求割边)

题目地址:codeforces #pi (DIV2) E 题目很水。。就是先求两边最短路,然后把可能为最短路的边挑出来,然后判断是否yes只需要转化成无向图跑一遍tarjan,找出割边,割边就是ye...

【Codeforces Round #327 (Div. 2)E】【最短路】 Three States 块联通

E. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standa...

Codeforces Round #303 (Div. 2) E. Paths and Trees (最短路+变形最小生成树)

题目地址:E. Paths and Trees 模拟了一场CF,这场实在太水了。。边玩边做的。。最后半分钟交了一发E题。。不幸AK绝杀失败。。。。 首先的思路肯定是先求最短路,把可能为最短路的边挑...

Codeforces Round #287 (Div. 2) C

507C - Guess Your Way Out!         题意:遍历一棵满二叉树,遍历方法是左右左右递归地遍历。树高度为h,最下层第n个节点是出口,问访问到出口前要访问多少个节点。 ...

[Codeforces] Round #287 (Div. 2) A、B

507A - Amr and Music 题意:给定一个n和k,分别代表Amr有n种乐器 和 他的最多学习天数k。接下来输入这n种乐器分别需要ai天才能学会。问Amr在这k天最多能学会的乐器数,并且把...

Codeforces Round #287 (Div. 2) C. Guess Your Way Out!

原题链接: http://codeforces.com/contest/507/problem/C C. Guess Your Way Out! time limit per...

Codeforces 507D (Round #287(div.2))D. The Maths Lecture【数位DP】

题目地址:http://codeforces.com/contest/507/problem/D 题意: 给出n,k,m,问满足一下条件的数字有多少: 1.这个数刚好有n位; 2.这个数存在某...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #287 (Div. 2) E. Breaking Good 最短路
举报原因:
原因补充:

(最多只允许输入30个字)