hdoj 3631 Shortest Path

來源:互聯網
上載者:User

 

Shortest Path

Time
Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K
(Java/Others)
Total Submission(s): 1386    Accepted Submission(s):
336

Problem DescriptionWhen YY was a boy and LMY was a girl, they trained for
NOI (National Olympiad in Informatics) in GD team. One day, GD team’s coach,
Prof. GUO asked them to solve the following shortest-path problem.
There is a
weighted directed multigraph G. And there are following two operations for the
weighted directed multigraph:
(1) Mark a vertex in the graph.
(2) Find the
shortest-path between two vertices only through marked vertices.
For it was
the first time that LMY faced such a problem, she was very nervous. At this
moment, YY decided to help LMY to analyze the shortest-path problem. With the
help of YY, LMY solved the problem at once, admiring YY very much. Since then,
when LMY meets problems, she always calls YY to analyze the problems for her. Of
course, YY is very glad to help LMY. Finally, it is known to us all, YY and LMY
become programming lovers.
Could you also solve the shortest-path
problem? 

 

InputThe input consists of multiple test cases. For each
test case, the first line contains three integers N, M and Q, where N is the
number of vertices in the given graph, N≤300; M is the number of arcs, M≤100000;
and Q is the number of operations, Q ≤100000. All vertices are number as 0, 1,
2, … , N - 1, respectively. Initially all vertices are unmarked. Each of the
next M lines describes an arc by three integers (x, y, c): initial vertex (x),
terminal vertex (y), and the weight of the arc (c). (c > 0) Then each of the
next Q lines describes an operation, where operation “0 x” represents that
vertex x is marked, and operation “1 x y” finds the length of shortest-path
between x and y only through marked vertices. There is a blank line between two
consecutive test cases.
End of input is indicated by a line containing N = M
= Q = 0. 

 

OutputStart each test case with "Case #:" on a single line,
where # is the case number starting from 1.
For operation “0 x”, if vertex x
has been marked, output “ERROR! At point x”.
For operation “1 x y”, if vertex
x or vertex y isn’t marked, output “ERROR! At path x to y”; if y isn’t reachable
from x through marked vertices, output “No such path”; otherwise output the
length of the shortest-path. The format is showed as sample output.
There is
a blank line between two consecutive test cases. 

 

Sample Input
5 10 101 2 63350 4 57253 3 69634 0 81461 2 99621 0 19432 1 23924 2 1542 2 74221 3 98960 10 30 20 40 40 11 3 31 1 10 30 40 0 0
 

 

Sample Output
Case 1:ERROR! At point 4ERROR! At point 100ERROR! At point 3ERROR! At point 4這是我在hdoj做的第一道題,以後要常來,不能只去poj。說一下演算法:這道題是變相的Floyd演算法,開始我寫了一個Floyd函數,o(n^3),當然會TLE。這裡的Floyd,中間的那個點由輸入提供,就不要寫三重迴圈了枚舉他了
 #include<iostream><br />using namespace std;<br />#define inf 0xfffffff//7個f,若是8個f ,後面a[ii][jj]>a[ii][t]+a[t][jj]<br /> // 判斷中a[ii][t]+a[t][jj]的和會溢出變為負值<br />int main()<br />{<br /> int a[400][400],mark[400];//它說N<=300,可開300就會RE,不知道為什麼<br /> int n,m,q;<br /> int cnt=0;<br /> while(scanf("%d%d%d",&n,&m,&q)==3&&!(n==0&&m==0&&q==0))<br /> {//m,q可以為0的,在這wa了好多次<br /> cnt++;<br /> if(cnt!=1)printf("/n");<br /> printf("Case %d:/n",cnt);//這個換行一定要判斷,且不能放到末尾,否則會多輸出空行<br /> for(int i=0;i<n;i++)<br /> {<br /> for(int j=0;j<n;j++)<br /> {<br /> if(i==j)a[i][j]=0;<br /> else a[i][j]=inf;<br /> }<br /> mark[i]=0;<br /> }</p><p> for(int i=0;i<m;i++)<br /> {<br /> int v,u,w;<br /> scanf("%d%d%d",&v,&u,&w);<br /> a[v][u]=min(a[v][u],w);<br /> }</p><p> for(int i=0;i<q;i++)<br /> {<br /> int op;<br /> scanf("%d",&op);<br /> if(op==0)<br /> {<br /> int t;<br /> scanf("%d",&t);<br /> if(mark[t])<br /> {<br /> printf("ERROR! At point %d/n",t);<br /> }<br /> else<br /> {<br /> mark[t]=1;<br /> for(int ii=0;ii<n;ii++)<br /> {<br /> for(int jj=0;jj<n;jj++)<br /> {<br /> if(a[ii][jj]>a[ii][t]+a[t][jj])<br /> {<br /> a[ii][jj]=a[ii][t]+a[t][jj];<br /> }<br /> }<br /> }<br /> }<br /> }<br /> else<br /> {<br /> int v,u;<br /> scanf("%d%d",&v,&u);</p><p> if(!mark[v]||!mark[u])<br /> {<br /> printf("ERROR! At path %d to %d/n",v,u);<br /> }<br /> else if(a[v][u]>=inf)<br /> {<br /> printf("No such path/n");<br /> }<br /> else<br /> {<br /> printf("%d/n",a[v][u]);<br /> }</p><p> }</p><p> }<br /> }<br />}<br />

2010-09-1610:07:32

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.